2016-11-30 28 views
0

我注意到iOS 10中一個奇怪的推送通知問題。我已經完成了this SO thread的推薦代碼更改以接收推送表單沙箱APNS。iOS 10推送通知 - 沙盒APNS問題

做出這些更改後,我從APNS獲取設備令牌並能夠接收通知。但是,如果我殺了&再次從Xcode啓動應用程序,推送不起作用。如果我從設備上刪除應用程序並將其從Xcode再次放入,則會推動開始。

任何人都看到了這種行爲,並知道可能的修復將是非常有用的。請指教。

這裏是在我的AppDelegate一步一步實現:

步驟1:導入UserNotifications

#import <UserNotifications/UserNotifications.h> 

步驟2:符合通知協議

@interface MyAppDelegate() <UIApplicationDelegate, UNUserNotificationCenterDelegate> 

第3步:在application:didFinishLaunchingWithOptions: register for notification

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10")) { 
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
      center.delegate = self; 
      [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { 
       if(!error){ 
        [[UIApplication sharedApplication] registerForRemoteNotifications]; 
       } 
      }]; 
     } 

步驟4:最後處理推送通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler { 
    [self handlePushNotificationWithUserInfo:response.notification.request.content.userInfo]; 
} 
+0

r你註冊了你的設備令牌 –

+0

從來沒有遇到過這個問題。嘗試重置模擬器。 –

+0

我收到推送令牌,交給我的服務器,並在服務器端註冊。他們也發送推送有效載荷。 – Abhinav

回答

1

處理通知在的iOS 10

有兩種方法。

willPresentNotification

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
{ 
    NSLog(@"This method is called to Handle push from foreground"); 
    // custom code to handle push while app is in the foreground 
    NSLog(@"User info ==> %@", notification.request.content.userInfo); 
} 

而且didReceiveNotificationResponse

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    didReceiveNotificationResponse:(UNNotificationResponse *)response 
    withCompletionHandler:(void (^)())completionHandler 
{ 
    NSLog(@"Handle push from background or closed"); 
    NSLog(@"%@", response.notification.request.content.userInfo); 
} 

希望它可以幫助.. !!