2016-07-23 5 views

Antwort

3

Sie können sich für Benachrichtigungen registrieren, wie Sie es bereits von iOS 8 getan haben (es ist eines der wenigen API für Benachrichtigungen, das sich nicht geändert hat).

Zuerst wird in der application:didFinishLaunchingWithOptions: Methode des AppDelegate, fordern die Berechtigung für Ihre App:

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

Dies wird die übliche Show „Anwendung möchten Sie Benachrichtigungen senden“ alert. Die Hauptverbesserung der neuen Methode requestAuthorization besteht darin, dass Sie das Verhalten des Antippens auf Schaltflächen Erlauben/Nicht zulassen direkt in den Schließungen verwalten können.

Als nächstes registrieren für Remote-Benachrichtigungen mit der registerForRemoteNotifications Methode von UIApplication erhältlich von iOS 8:

UIApplication.shared().registerForRemoteNotifications() 

... und schließlich die Registrierung mit Ihrem Benachrichtigungen Server verwalten (wie Amazon, OneSignal, etc ...

)
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

mit Objective-C Methode:

- (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 
    } 

Hier ist eine Demo: iOS10AdaptationTips.