首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 10推送通知-沙箱APNS问题

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

Stack Overflow用户
提问于 2016-11-30 04:23:35
回答 1查看 318关注 0票数 0

我注意到iOS 10中有一个奇怪的推送通知问题,我建议在this SO thread中修改代码以接收推送表单沙箱APNS。

在进行这些更改之后,我将从APNS获得设备令牌,并能够接收通知。但是,如果我再次从Xcode中杀死和重新启动应用程序,则推送不起作用。如果我从设备中删除该应用程序,并将其从Xcode中再放一次,请按下“开始”。

任何人见过这种行为,并知道可能的解决办法,将是非常有帮助的。请给我建议。

下面是我的AppDelegate中一步一步的实现

步骤1:导入UserNotifications

代码语言:javascript
复制
#import <UserNotifications/UserNotifications.h>

步骤2:遵守通知协议

代码语言:javascript
复制
@interface MyAppDelegate() <UIApplicationDelegate, UNUserNotificationCenterDelegate>

application:didFinishLaunchingWithOptions: register for notification中的步骤3:

代码语言:javascript
复制
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:最后处理推送通知

代码语言:javascript
复制
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    [self handlePushNotificationWithUserInfo:response.notification.request.content.userInfo];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-30 05:24:15

处理iOS 10中的通知。

有两种方法。

willPresentNotification

代码语言:javascript
复制
- (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

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

希望能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40880263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档