我决定从我的应用程序中关闭关于“提醒”切换控件的通知。我在我的应用程序中添加了这些行以使其同时支持 iOS 7 和 iOS 8,并且当我关闭推送通知时它可以正常工作。但是当我决定打开开关并关闭应用程序时,当我再次打开它时它又回到了关闭状态而不是打开状态。所以我必须转到设置 --> 通知中心 --> “我的应用程序”,然后重新打开所有东西,因为它们都关闭了……很奇怪,当我在 iOS 7 上测试它时,它可以工作,但不适用于 iOS 8. 任何建议表示赞赏。谢谢。
- (IBAction)reminderSwitchToggled:(id)sender {
UIApplication *application = [UIApplication sharedApplication];
if ([sender isOn]) {
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else
#endif
{
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeSound];
}
} else {
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:
(UIUserNotificationType)
(UIUserNotificationTypeNone)
categories:nil]];
[application unregisterForRemoteNotifications];
}
else
#endif
{
[application unregisterForRemoteNotifications];
}
}
}
最佳答案
您应该在服务器端实现禁用推送通知。 Apple 文档说你应该调用
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
仅当您不再在应用程序中提供通知时。 Link
另一方面,iOS8 提供了打开 Settings.app 的 API,用户可以在其中使用开关控件禁用您应用的通知。使用示例:
if (&UIApplicationOpenSettingsURLString != NULL)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
关于ios - unregisterForRemoteNotifications 不适用于 iOS8 - 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25457655/