2016-07-23 39 views

回答

3

您可以註冊通知,例如您已經從iOS 8(這是少數幾個未更改的通知API)之一執行的操作。

首先,在AppDelegate中的application:didFinishLaunchingWithOptions:方法,要求授權爲您的應用程序:

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

這將顯示通常的「應用程序想向你發送通知」的警報。新的requestAuthorization方法的主要改進是您可以管理在關閉中直接點擊「允許/不允許」按鈕的行爲。

接下來,註冊遠程通知與UIApplicationregisterForRemoteNotifications方法可以從iOS的8:

UIApplication.shared().registerForRemoteNotifications() 

...最後管理與您的通知服務器註冊(如亞馬遜,OneSignal等.. )

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

隨着Objective-C的方法:

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