【问题标题】:iOS 10 Push Notifications - Sandbox APNS IssueiOS 10 推送通知 - 沙盒 APNS 问题
【发布时间】:2016-11-30 04:23:35
【问题描述】:

我注意到 iOS 10 存在一个奇怪的推送通知问题。我已在 this SO thread 中进行了建议的代码更改,以接收来自沙盒 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];
                }
            }];
        }

第四步:最后处理推送通知

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

【问题讨论】:

  • 你注册了你的设备令牌
  • 从未遇到过这个问题。尝试重置模拟器。
  • 我收到推送令牌,交给我的服务器并在服务器端注册。他们也在发送推送有效载荷。
  • 你能显示你的appdelegate代码吗
  • 请检查您的设备令牌是否正确注册或正确?您如何测试 Push for Sandbox APNS?

标签: ios objective-c push-notification ios10


【解决方案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);
}  

希望对您有所帮助..!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2013-06-11
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多