2016-07-23 7 views

答えて

3

あなたは既にiOS 8からやっているような通知に登録できます(変更されていない通知のための少数のAPIの1つです)。

まず、AppDelegateのapplication:didFinishLaunchingWithOptions:方法では、アプリの承認を要求します。

UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in 
    //here you can check the correct authorization  
} 

これは、通常の警告「アプリケーションは、あなたに通知を送信したいと思います」と表示されます。新しいrequestAuthorizationメソッドの主な改善点は、クロージャで[許可/禁止]ボタンを直接タップする動作を管理できることです。などアマゾン、OneSignal、同様に(

UIApplication.shared().registerForRemoteNotifications() 

...そして最終的にあなたの通知サーバへの登録を管理...:

次に、iOSの8から入手UIApplicationregisterForRemoteNotifications方法を使用して、リモート通知を登録します:Objective-Cの方法で)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    //if you need the token as a string, do this: 
    let tokenString = String(data: deviceToken, encoding: .utf8) 

    //call the notifications server for sending the device token 
} 

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    print("Application failed to register for remote notifications") 
} 

Reference

1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     [self registerForRemoteNotification]; 
     . . . 
    } 


    - (void)registerForRemoteNotification { 
     if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { 
      UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter]; 
      [uncenter setDelegate:self]; 
      [uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound) 
            completionHandler:^(BOOL granted, NSError * _Nullable error) { 
             [[UIApplication sharedApplication] registerForRemoteNotifications]; 
             NSLog(@"%@" , granted ? @"success to request authorization." : @"failed to request authorization ."); 
            }]; 
      [uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { 
       NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings); 
       if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) { 
        //TODO: 
       } else if (settings.authorizationStatus == UNAuthorizationStatusDenied) { 
        //TODO: 
       } else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { 
        //TODO: 
       } 
      }]; 
     } 
    #pragma clang diagnostic push 
    #pragma clang diagnostic ignored "-Wdeprecated-declarations" 
     if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { 
      UIUserNotificationType types = UIUserNotificationTypeAlert | 
              UIUserNotificationTypeBadge | 
              UIUserNotificationTypeSound; 
      UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 

      [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
      [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     } else { 
      UIRemoteNotificationType types = UIRemoteNotificationTypeBadge | 
              UIRemoteNotificationTypeAlert | 
              UIRemoteNotificationTypeSound; 
      [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types]; 
     } 
    #pragma clang diagnostic pop 
    } 

デモ:iOS10AdaptationTipsです。

関連する問題