iOS 8+ローカル通知

2270 ワード

iOS 8から、新しい機能が追加されたことを通知します.簡単に言えば、これから、通知が表示されると、開発者は、ユーザがトリガ可能な具体的な動作(actions)を指定することができ、Appを起動することなく、この通知を処理することができる.
ユーザーに通知をプロンプトする方法はいくつかあります.次に、サポートされているすべての通知タイプを示します.ご存じのように、通知タイプを彼らのいくつかまたはすべてに指定できます.
  • Alert or Banner:通知はalertまたはbannerで表示できます.これは、ユーザーが設定で選択するかどうかによって異なります.彼らは通知のメッセージを含まなければならない(もちろんローカライズすることができる).
  • サウンド(Sound):通知が届くと、iOSにカスタムまたはシステムのデフォルトのサウンドを「伝える」ことができます.ユーザはデバイスをずっと見ていないため,表示された通知が無視される可能性があるため,音声が有用である.しかし、重要でない通知に対しては、音声は使用されるべきではない.
  • Badge:通知が到着すると、Appのアイコンにbadge数字が表示されます.通知が到着すると、badge数字は必ず1増加し、通知が処理されるとbadge数字は1減少する.badgeの数字が0または0でない場合、iOSはbadgeを表示または非表示にします.

  • iOS 8以降では、以前のローカルプッシュの書き方でエラーが発生する可能性があり、プッシュの情報が受信できない場合は、以下の情報が表示されます.
    1 Attempting to schedule a local notification
    2 with an alert but haven't received permission from the user to display alerts
    3 with a sound but haven't received permission from the user to play sounds
    

    IOS 8には登録されていないので、IOS 8への登録方法を追加する必要があります.APIには次の方法があります.
    // Registering UIUserNotificationSettings more than once results in previous settings being overwritten.  
    - (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);  
    

    ios 8の後、この登録を追加する必要があります.
      if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
                                                                                 categories:nil];  
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
        //  , 、 、   
        notification.repeatInterval = NSCalendarUnitDay;  
      } else {  
        //  , 、 、   
        notification.repeatInterval = NSDayCalendarUnit;  
      }
    

    アプリケーションがフロントにあるときに呼び出されるコールバック関数をローカルで通知します.このメソッドはAppDelegateで使用されます.
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
    
    

    再プッシュが不要な場合は、cancelLocalNotificationWithKeyのプッシュをキャンセルできます.