iOS 10推送通知 - 沙箱APNS问题

时间:2016-11-30 04:23:36

标签: ios objective-c push-notification ios10

我注意到iOS 10的一个奇怪的推送通知问题。我已经在this SO thread中完成了建议的代码更改,以便从沙箱APNS接收推送。

进行这些更改后,我从APNS返回设备令牌并能够接收通知。但是,如果我杀了&再次从Xcode重新启动应用程序,推送不起作用。如果我从设备中删除该应用程序并从Xcode再次放入该应用程序,则推送开始。

任何人都看过这种行为并且知道可能的解决方案会非常有用。请指教。

以下是我AppDelegate中的逐步实施:

第1步:导入用户通知

#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];
}

1 个答案:

答案 0 :(得分: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);
}  
  

并且确认接收通知响应

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

希望它有所帮助.. !!