iOS 10-仅首次调用didRegisterForRemoteNotificationsWithDevice [英] iOS 10 - didRegisterForRemoteNotificationsWithDevice only called the first time
问题描述
我遇到的问题是,在iOS 10上重新安装应用程序后,我第一次调用registerForRemoteNotifications时,didRegisterForRemoteNotificationsWithDevice仅在第一次调用时被调用.
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发生了更改,其中调用取消注册会导致注册在一段时间后无法正常工作.
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错误,还有更多开发人员抱怨在这里遇到类似的问题:
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
didRegisterForRemoteNotificationsWithDeviceToken
- nor
didFailToRegisterForRemoteNotificationsWithError
方法.基本上,您不会从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屋!