我注意到iOS 10中有一个奇怪的推送通知问题,我建议在this SO thread中修改代码以接收推送表单沙箱APNS。
在进行这些更改之后,我将从APNS获得设备令牌,并能够接收通知。但是,如果我再次从Xcode中杀死和重新启动应用程序,则推送不起作用。如果我从设备中删除该应用程序,并将其从Xcode中再放一次,请按下“开始”。
任何人见过这种行为,并知道可能的解决办法,将是非常有帮助的。请给我建议。
下面是我的AppDelegate中一步一步的实现
步骤1:导入UserNotifications
#import <UserNotifications/UserNotifications.h>步骤2:遵守通知协议
@interface MyAppDelegate() <UIApplicationDelegate, UNUserNotificationCenterDelegate>application:didFinishLaunchingWithOptions: register for notification中的步骤3:
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];
}发布于 2016-11-30 05:24:15
处理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);
} 希望能帮上忙!
https://stackoverflow.com/questions/40880263
复制相似问题