APNS - 获取设备令牌
我将使用 AppNotify 服务在我的应用程序中实现推送通知。要完成该服务的设置,我需要获取我的设备令牌。我尝试过使用苹果文档中的代码。在模拟器上我收到一个错误(当然是预料之中的)。在我的设备上,我没有收到错误,但也没有收到令牌。两个委托方法都没有被调用。这是代码(第一位位于 applicationDidFinishLaunching 中):
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
//const void *devTokenBytes = [devToken bytes];
//self.registered = YES;
//[self sendProviderDeviceToken:devTokenBytes]; // custom method
NSLog(@"Success");
NSLog(@"Token = %@", devToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
有什么想法为什么会发生这种情况吗?
谢谢
I am going to implement push notifications in my app with the AppNotify service. To finish setting that service up I need to get my device token. I have tried using the code from Apple's docs. On the simulator I get an error (expected of course). On my device I do not get an error, but I do not get the token either. Neither delegate method is called. Here is the code (first bit goes in applicationDidFinishLaunching):
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
//const void *devTokenBytes = [devToken bytes];
//self.registered = YES;
//[self sendProviderDeviceToken:devTokenBytes]; // custom method
NSLog(@"Success");
NSLog(@"Token = %@", devToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
Any ideas why this is happening?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
曾经,您必须知道远程通知在模拟器上不起作用。
问候
Once, you must know that remote notifications doesn't work on simulator.
Regards
还要添加以下方法:
如果您编写了正确的代码(如您所示),则会调用上述 3 种方法之一(您编写的 2 种方法和我上面的方法)。如果设备上没有出现任何错误,请等待一段时间(理想情况下是 10 秒,但也可能需要更长的时间),以便调用 didRegisterForRemoteNotificationsWithDeviceToken 方法。问候。
add the following method as well:
If you have written the right code (as you show) one of the above 3 methods (the 2 you have written and the one i have above) gets called. If you don't get any error on the device then wait for some time (ideally 10 seconds but it maybe take longer as well) for the didRegisterForRemoteNotificationsWithDeviceToken method to get called. Regards.