我正在开发IOS推送通知,我试图抓住设备令牌。我可以在操作系统通知设置打开时获取设备令牌。当它关闭时,我的didRegisterForRemoteNotificationsWithDeviceToken
不再被调用,因此,我无法获得设备令牌。
这是我的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
return YES;
}
我还添加了
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
但是,关闭操作系统通知设置时,
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
不会被调用。我已经调试了好几个小时而没发现运气。
答案 0 :(得分:0)
我遇到了同样的问题。
通过在项目的功能部分中启用“远程通知”来管理解决它(请参见下面的屏幕截图)。
我对此问题的看法:
如果用户明确禁止来自您的应用程序的通知,并且应用程序已关闭“远程通知”,则您无法向用户发送通知。这就是为什么不需要为您的应用程序返回令牌。
如果用户已关闭通知,但您启用了“远程通知”,则表示您仍有机会发送无提示通知,iOS会为您提供令牌。< / p>