Enable/Disable Apple Push Notification from iPhone app?

廉价感情. 提交于 2019-11-27 12:47:08

Unfortunately you can't enable or disable the Push Notifications for your app from the app code. The dialog asking for permission is displayed only once. Usually, other apps display instructions to the user to enable / disable push notifications by going into Settings-> Notification-> AppName.

You can read your app's permissions using UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; and then performing a bitwise and operation with the different types to see which are enabled. You can also call unregisterForRemoteNotifications to disable notifications. The one thing you can't do is turn on notifications, although you can direct the user.

arshad

My requirement was to enable and disable pushnotificaton with a UISwitch.Inorder to enable push notification from the code use this inside the button action.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
   (UIRemoteNotificationTypeBadge |  UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

Inorder to disable

[[UIApplication sharedApplication] unregisterForRemoteNotifications];
NSLog(@"UnRegistered for pushnotification");

1.From your app No its just appear the first time the user open your app after install it .. if then he decide to allow it he can activated from device settings.

2.it can be done from the app and settings .. if you want to disable it from your app you could send the device token (who decide to disable the push notification) to your server and store it for ex. as "no notification list" and when send the payload you ignore these tokens so they will not receive the notification.

3.I already answer it.

Good Luck.

wasim

When you give permission for the first time the device token of your iPhone is registered with the APN server and then you can receive the push notification. Later you can enable/disable from your device settings → notification → your app.

debiasej

You can use this code to give support in iOS 9

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {

    UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];

    if (types == UIUserNotificationTypeNone) {
        // Do something
        NSLog(@"");
    }
} else {

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    if (types == UIRemoteNotificationTypeNone) {
        // Do something
        NSLog(@"");
    }
}

see How to update code using enabledRemoteNotificationTypes because it is "not supported in iOS 8"?

Pragmatically, it is possible to enable & disable push notification by registering and unregistering push notification.

Enable Push Notification:

if #available(iOS 10.0, *) {
   // For iOS 10.0 +
   let center  = UNUserNotificationCenter.current()
   center.delegate = self
   center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
        if error == nil{
           DispatchQueue.main.async(execute: {
                 UIApplication.shared.registerForRemoteNotifications()
           }) 
        }
   }
}else{
    // Below iOS 10.0

    let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)

    //or
    //UIApplication.shared.registerForRemoteNotifications()
}

Delegate methods

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // .. Receipt of device token
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // handle error
}

Disable Push Notification:

UIApplication.shared.unregisterForRemoteNotifications()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!