iOS如何检查用户是否已经禁用推送通知

3

我希望确认用户是否在我的应用中选择了禁用推送通知功能。

我调用以下代码:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

每次启动应用程序时,我都想将一些信息发送回服务器。但是,在用户不允许我的应用程序发送推送通知的情况下,我希望能够向服务器发送一些信息。
我在思考将该逻辑放置在此回调函数中:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

然而,我不确定当用户禁用应用程序接收推送通知时,是否会触发此回调,或者只是一个错误情况(连接错误等)。如果是这样,我应该寻找什么错误?


好问题。尝试在标准用户默认设置中维护和保存BOOL值。 - user2223516
1个回答

2

请查看以下代码,它将帮助您:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (types == UIRemoteNotificationTypeNone) {
    // send info to the server that this user has disallowed push notifications
}

1
这个不起作用。如果你禁用了推送通知但没有关闭各个开关,你仍会收到大于0的值。 - Julian F. Weinert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接