iOS 10 - didRegisterForRemoteNotificationsWithDevice 仅第一次调用 [英] iOS 10 - didRegisterForRemoteNotificationsWithDevice only called the first time

查看:27
本文介绍了iOS 10 - didRegisterForRemoteNotificationsWithDevice 仅第一次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,didRegisterForRemoteNotificationsWithDevice 仅在我在 iOS 10 上重新安装应用程序后第一次调用 registerForRemoteNotifications 时才被调用.

I'm having a problem where didRegisterForRemoteNotificationsWithDevice only gets called the first time I call registerForRemoteNotifications after reinstalling the app, on iOS 10.

发生了什么:卸载应用程序,然后从 XCode 调试运行应用程序后,我在应用程序内弹出窗口中点击允许",我确实收到了确实使用设备令牌注册了远程通知!"的日志消息.,然后我得到一个设备令牌.这让我犹豫地认为我的证书/配置文件没问题.但是,此设备令牌似乎不适用于发送推送通知(可能是服务器上的一个单独问题),关闭应用程序并重新打开后,我只看到已授予推送授权:1"和推送注册开始"."但没有允许推送通知的弹出窗口,也没有 didRegister 回调/令牌.

What's happening: after uninstalling the app and then running the app in debug from XCode, I tap "allow" on the in-app popup and I do get my log message for "did register for remote notifications with device token!", and I get a device token. This makes me hesitantly think that my certificate / provisioning profiles are OK. However, this device token doesn't seem to work for sending push notifications (might be a separate issue on the server), and after closing the app and reopening it, I only see "Push authorization granted: 1" and "Push registration starting." but no popup to allow push notifications, and no didRegister callback/token.

我尝试寻找解决方案,但我找不到任何人的 didRegister 函数只在第一次显示,所以我不确定是什么原因造成的.

I tried finding solutions, but I couldn't find anyone for whom the didRegister function only shows the first time so I'm not sure what could be causing it.

这是应用程序使用的代码:

This is the code that the app is using:

-(void)registerForNotifications
{
#if !TARGET_IPHONE_SIMULATOR
if(SYSTEM_VERSION_LESS_THAN(@"10.0")) {
    // This part is irrelevant, doesn't get called on iOS 10
    -> some code for push notifications
}
else {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
     {
         NSLog(@"push authorization granted: %d", granted);
         if(!error)
         {
             [[UIApplication sharedApplication] registerForRemoteNotifications];
             NSLog(@"Push registration starting.");
         }
         else
         {
             NSLog(@"Push registration FAILED");
             NSLog(@"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription);
             NSLog(@"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion);
         }  
     }];
}
#endif
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDevice :(NSData *)deviceToken
{
    NSLog(@"did register for remote notifications with device!");
    [self handleNewDeviceToken:deviceToken];
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
{
    NSLog(@"did register for remote notifications with device token!");
    [self handleNewDeviceToken:deviceToken];
}

我希望这对我来说只是一些愚蠢的事情/提供配置文件或证书,我对 iOS 开发还不太有经验.如果您可以使用任何其他信息,请告诉我.

I'm hoping this is just something stupid on my side / with provisioning profiles or certificates, I'm not too experienced with iOS development yet. Do let me know if you could use any additional information.

编辑好吧,根本问题是我的代码每次在调用 register 之前都调用 unregisterForRemoteNotifications.显然,iOS10 有一个变化,调用 unregister 会导致 register 在一段时间后无法工作.

EDIT Alright, the underlying issue was that my code was calling unregisterForRemoteNotifications every time before calling register. Apparently, iOS10 had a change where calling unregister causes register not to work for a bunch of time after.

推荐答案

这看起来是一个 iOS 10 错误,更多的开发者抱怨在这里遇到类似的问题:https://forums.developer.apple.com/thread/63038.

This looks lie an iOS 10 bug and couple of more developers are complaining to experience similar issue here: https://forums.developer.apple.com/thread/63038.

据我所知,在您调用 unregisterForRemoteNotifications 之后,随后尝试调用 registerForRemoteNotifications不会调用任何:

From what I see, the subsequent attempts to call registerForRemoteNotifications after you have called unregisterForRemoteNotifications will not invoke any of:

  • didRegisterForRemoteNotificationsWithDeviceToken
  • 也没有 didFailToRegisterForRemoteNotificationsWithError

AppDelegate 上的方法.基本上你不会收到来自 iOS 的任何回调.

methods on AppDelegate. Basically you don't receive any callback from the iOS.

我目前知道的唯一解决方法是向您的 Info.plist 添加后台获取功能.我可以确认这为我解决了这个问题,但是您需要评估该解决方案是否适合您.

The only workaround that I am currently aware of, is to add background fetch capability to you Info.plist. I can confirm that this solved the issue for me, but you would need to evaluate whether that solution os plausible for you.

这篇关于iOS 10 - didRegisterForRemoteNotificationsWithDevice 仅第一次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆