iOS9中註冊遠程通知的方法
在以往的版本中,我們可以通過:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:some types]
來註冊遠程通知,不過很快該方法就被標記為廢棄,並且提示我們要用另一個類似的方法:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
別高興的太早,雖然registerUserNotificationSettings方法現在照樣工作,不過其中的諸如UIRemoteNotificationTypeXXX之類的類型很快又被標記為廢棄…
好吧,在iOS9中,註冊遠程通知變成了如下類似的代碼:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeBadge|UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications];
額,你沒看錯,以上代碼在註冊遠程通知的同時還註冊了本地通知,所以我們可以理解為遠程通知只是在原有本地通知基礎上開啟了遠程功能.不過這隻是我個人的理解,歡迎錯誤修正. ;)