ios - 完成启动后或在我们的应用程序内启用/禁用推送通知

标签 ios objective-c iphone push-notification

完成启动后,我需要在我们的应用程序内启用/禁用或注册/取消注册推送通知。我将在启动后在我的应用程序“帐户设置”中执行此功能

我尝试了下面的代码,但它不起作用。

 if (cell.switchButton.on) {
    if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}
else {
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}

我的要求:我将以编程方式在我的应用程序内启用或禁用通知切换,而不是直接转到设置 -> 通知。

最佳答案

您不应该使用unregisterForRemoteNotifications,它会像苹果所说的那样完全取消注册APNS

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app.

这里是link

如果您在服务器端实现禁用推送通知,效果会更好。只是告诉您的服务器人员让服务不发送 APN。

或者您也可以打开设置应用程序的通知部分,如下所示

if (&UIApplicationOpenSettingsURLString != NULL){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

关于ios - 完成启动后或在我们的应用程序内启用/禁用推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39406398/

相关文章:

iphone - ios label.text null

ios - Xamarin.Forms 和 iOS : how to combine UseSafeArea and a background image?

ios - quickBlox iOS SDK 的委托(delegate)问题真的很奇怪

iphone - GPS不会停止更新

iphone - 屏蔽的 UIView 上的 UIGestureRecogniser?

ios - 如何在导航 Controller 中同时进行模式转换和弹出到根?

ios - 无法在UITableView中删除indexpath

ios - 使用自动布局以编程方式将 subview 添加到 UIView 时来源错误

iphone - 如何获取 iTunes 应用程序描述?

ios - PromiseKit 中的 promise 链接