對於使用xCode 8 GM的iOS 10。
此問題通過以下步驟解決。 要求: - Xcode 8轉基因種子。 MAC操作系統: - 船長EL 10.11.6
不要刪除您的IOS 9或以下版本的代碼。
步驟1: - 進入 - >目標設置在Xcode - >功能 - >啓用PushNotifications。
步驟2: - 添加UserNotifications框架 - >構建階段 - >鏈接庫
步驟3: -
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
步驟4: - 在方法didFinishLaunchingWithOptions註冊UIUserNotificationSettings。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if(SYSTEM_VERSION_EQUALTO(@"10.0")){
UNUserNotificationCenter *notifiCenter = [UNUserNotificationCenter currentNotificationCenter];
notifiCenter.delegate = self;
[notifiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
return YES;
}
第5步: -實施UNUserNotificationCenterDelegate的2種委託方法。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
//Do Your Code.................Enjoy!!!!
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
}
您是如何註冊通知的? 'UIApplication'使用'-registerUserNotificationSettings:'和'-registerForRemoteNotifications'註冊了嗎?後者需要調用'didRegisterForRemoteNotificationsWithDeviceToken'。請參閱https://developer.apple.com/reference/uikit/uiapplication/1623078-registerforremotenotifications。 – Roy
也許你可以從這裏獲得幫助。 https://www.sitepoint.com/developing-push-notifications-for-ios-10/ –
你可以按照上面的步驟來獲取工作推送通知在iOS 10 http://stackoverflow.com/questions/39572897/ IOS-10推送通知-問題,解決 –