xcode didRegisterForRemoteNotificationsWithDeviceToken 从未在特定设备上调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26817810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didRegisterForRemoteNotificationsWithDeviceToken never called on specific device
提问by Can Poyrazo?lu
First of all, I am on iPhone 6 Plus/iOS 8.1 and I've tried everything here: why didRegisterForRemoteNotificationsWithDeviceToken is not called
首先,我在 iPhone 6 Plus/iOS 8.1 上,我在这里尝试了一切:为什么没有调用 didRegisterForRemoteNotificationsWithDeviceToken
Still no avail. To summarize:
还是没有用。总结一下:
- I've checked my bundle identifier matches the one in provisioning profile/development portal
- I've created a new development push APNS certificate
- I've created a new development certificate
- I've created a new provisioning profile for that development certificate
- I've downloaded both the certificate and provisioning profile, and obviously, double clicked them to install
- I've verified that everything is right on Developer Portal: all certificates and provisioning profiles valid, push enabled and configured with my new APNS certificate
- I've uploaded my new APNS certificate to Parse (it's irrelevant at this step, but anyway) as I'm using Parse for my backend
- I've made sure that I'm using the correct certificate/provisioning profile pair at Xcode to codesign
- I've checked Notifications settings in case my app is not allowed to receive pushes, it's not there
- I've tried setting date manually to tomorrow and tried re-installing the app
- I've deleted the app from my device
- I've deleted any related provisioning profiles from my device
- I've restarted my device multiple times
- 我检查了我的包标识符与配置文件/开发门户中的标识符匹配
- 我创建了一个新的开发推送 APNS 证书
- 我创建了一个新的开发证书
- 我为该开发证书创建了一个新的配置文件
- 我已经下载了证书和配置文件,显然,双击它们进行安装
- 我已经在 Developer Portal 上确认一切正常:所有证书和配置文件都有效、启用推送并使用我的新 APNS 证书进行配置
- 我已将我的新 APNS 证书上传到 Parse(这与这一步无关,但无论如何),因为我将 Parse 用于我的后端
- 我已确保在 Xcode 中使用正确的证书/配置文件对进行协同设计
- 我检查了通知设置以防我的应用程序不允许接收推送,它不存在
- 我尝试将日期手动设置为明天并尝试重新安装该应用程序
- 我已从我的设备中删除了该应用
- 我已从我的设备中删除了所有相关的配置文件
- 我已经多次重启我的设备
In application:didFinishLaunchingWithOptions:
I call:
在application:didFinishLaunchingWithOptions:
我打电话:
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
//iOS 8+
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}else{
//pre-iOS 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
In application:didRegisterUserNotificationSettings:
I call:
在application:didRegisterUserNotificationSettings:
我打电话:
[application registerForRemoteNotifications];
I've checked it with a breakpoint, it DOES get called. I've also implemented two methods:
我已经用断点检查过它,它确实被调用了。我还实现了两种方法:
application:didRegisterForRemoteNotificationsWithDeviceToken:
application:didRegisterForRemoteNotificationsWithDeviceToken:
and
和
application:didFailToRegisterForRemoteNotificationsWithError:
application:didFailToRegisterForRemoteNotificationsWithError:
However, neither of them is called. Not an error, nothing. No error at the console either. (I've had an issue about entitlements earlier today, but creating new certificate/provisioning profile solved that)
但是,它们都没有被调用。不是错误,什么都没有。控制台也没有错误。(我今天早些时候遇到了一个关于权利的问题,但创建新的证书/配置文件解决了这个问题)
What could be the issue?
可能是什么问题?
UPDATE:Here is something. In application:didRegisterUserNotificationSettings:
I've checked the notification settings and here is what I've got:
更新:这里有一些东西。在application:didRegisterUserNotificationSettings:
我检查了通知设置,这是我得到的:
(lldb) po notificationSettings
<UIUserNotificationSettings: 0x170031cc0; types: (none);>
UPDATE 2:I've checked notifications again, and found out that now my app is added to the notifications in Settings, it's enabled, given permissions. But still, the handler is not called. Types are none. I'm 99% sure it's related to the problem.
更新 2:我再次检查了通知,发现现在我的应用程序已添加到“设置”中的通知中,它已启用并授予权限。但仍然没有调用处理程序。类型是无。我 99% 确定它与问题有关。
UPDATE 3:I've tested on another device (iPod touch, iOS 8.1), and I've got no problems there. It immediately called the application:didRegisterForRemoteNotificationsWithDeviceToken:
method with its token. The problem is specific to my iPhone.
更新 3:我已经在另一台设备(iPod touch、iOS 8.1)上进行了测试,我没有遇到任何问题。它立即application:didRegisterForRemoteNotificationsWithDeviceToken:
使用其令牌调用该方法。该问题特定于我的 iPhone。
回答by Yuri Natividade
I got a similar problem, the code was already implemented and working fine. Suddenly, after some adjustments, it just don't work it anymore.
我遇到了类似的问题,代码已经实现并且运行良好。突然,经过一些调整后,它不再起作用了。
The scenario running on device was:
在设备上运行的场景是:
- Call registerForRemoteNotificationson didFinishLaunchingWithOptions.
- The didRegisterForRemoteNotificationsWithDeviceTokenwon't call
- nor didFailToRegisterForRemoteNotificationsWithErrorwon't call.
- 在didFinishLaunchingWithOptions上调用registerForRemoteNotifications。
- 该didRegisterForRemoteNotificationsWithDeviceToken不会叫
- 也不didFailToRegisterForRemoteNotificationsWithError不会叫。
I tried everything, almost reset all push configurations and certificates and provisioning profiles, etc.. All devices with the last version was working, but when I install the new version, just don't work anymore.
我尝试了所有方法,几乎重置了所有推送配置和证书以及配置文件等。使用最新版本的所有设备都可以工作,但是当我安装新版本时,就不再工作了。
To solve this, I just did this:
为了解决这个问题,我只是这样做了:
- Went to the target capabilities;
- Turn the Push NotificationOff;
- Build& Runthe app on device and wait;
- Stop running the app;
- Turn the Push NotificationOnagain.
- Build& Runthe app on device;
- And like magic, it worked again
- 去目标能力;
- 关闭推送通知关闭;
- 在设备上构建并运行应用程序并等待;
- 停止运行应用程序;
- 关闭推送通知上一次。
- 在设备上构建和运行应用程序;
- 就像魔法一样,它又奏效了
After 6h fighting with Xcode, that was my solution, without any explanation.
在与 Xcode 斗争 6 小时后,这就是我的解决方案,没有任何解释。
I hope this help someone.
我希望这对某人有帮助。
回答by ader
I was asked to debug similar behaviour on a clients app, under iOS9.
我被要求在 iOS9 下的客户端应用程序上调试类似的行为。
It turns out the app was calling registerUserNotificationSettings and registerForRemoteNotifications from the appDelegate as is common.
事实证明,该应用程序正在从 appDelegate 调用 registerUserNotificationSettings 和 registerForRemoteNotifications,这很常见。
However, it was also doing this a second time shortly after in a subsequent permissionsViewController.
但是,它也在随后的权限视图控制器中第二次执行此操作。
If a user declined the request resulting from the appDelegate then the subsequent attempts to allow notifications after a user had seen a rationale for allowing it (the second calls from the permissionsViewController) were set with types set to zero same as the appDelegate calls.
如果用户拒绝了由 appDelegate 产生的请求,则在用户看到允许通知的理由(来自 permissionsViewController 的第二次调用)之后,随后尝试允许通知的类型设置为零,与 appDelegate 调用相同。
Removing the initial appDelegate calls and having only the permissionsViewController calls present solved this.
删除初始 appDelegate 调用并仅存在 permissionsViewController 调用解决了这个问题。
回答by Brett
Another thing to check is the system status of APNS at Apple https://developer.apple.com/system-status/.
要检查的另一件事是 Apple https://developer.apple.com/system-status/上 APNS 的系统状态。
I was tearing my hair out wondering why I couldn't register my device while the APNS system was 'experiencing problems'.
当 APNS 系统“遇到问题”时,我想知道为什么我无法注册我的设备。
回答by MetaSnarf
try using this: (works well), sorry stack doesn't seem to format this well
尝试使用这个:(效果很好),抱歉堆栈似乎没有很好地格式化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"iOS 8 Requesting permission for push notifications..."); // iOS 8
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
UIUserNotificationTypeSound categories:nil];
[UIApplication.sharedApplication registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
NSLog(@"iOS 7 Registering device for push notifications..."); // iOS 7 and earlier
[UIApplication.sharedApplication registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSLog(@"received a notification while active...");
}
else if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
NSLog(@"i received a notification...");
NSLog(@"Message: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"whole data: %@",userInfo);
}
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
self.token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
self.token = [self.token stringByReplacingOccurrencesOfString:@" " withString:@""];
[[NSUserDefaults standardUserDefaults] setObject:self.token forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"-->> TOKEN:%@",self.token);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
NSLog(@"Registering device for push notifications..."); // iOS 8
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 s
completionHandler();
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
// Respond to any push notification registration errors here.
NSLog(@"Failed to get token, error: %@", error);
}