0

我正在开发 IOS 推送通知,并试图获取设备令牌。打开操作系统通知设置时,我能够获取设备令牌。当它关闭时,didRegisterForRemoteNotificationsWithDeviceToken不再调用 my ,因此我无法获取设备令牌。

这是我的代码

- (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

不会被调用。我已经调试了很多小时,但没有发现运气。

4

1 回答 1

0

我遇到了同样的问题。

通过在项目的功能部分中启用“远程通知”来解决它(见下面的截图)。

背景模式

我对这个问题的看法:

如果用户已明确禁止来自您的应用程序的通知并且应用程序已关闭“远程通知”,则您无法向用户发送通知。这就是为什么不需要为您的应用程序返回令牌的原因。

如果用户已关闭通知,但您启用了“远程通知”,则意味着您仍有机会发送静默通知,iOS 为您提供令牌。

于 2015-10-31T09:45:28.810 回答