Enable or Disable Iphone Push Notifications inside the app

南笙酒味 提交于 2019-11-27 07:16:51

First thing is that you can not enable and disable push notification in inside the app. If you have found some apps which did it than there must be workaround solution.

Like if you want to do Inside the app then use one identifier and send it to server according push notification enable and disable button. So, your server side coding use this identifier and work according to that. Like identifier is say it's enable than your server will send notification otherwise not.

You can check that user set enable or disable Push Notifications using following code.

Enable or Disable Iphone Push Notifications

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
 // Yes it is..

Hope, this will help you..

[FYI - Few users have reported that it stopped working on iOS 10]

You can easily enable and disable push notifications in your application by calling registerForRemoteNotificationTypes and unregisterForRemoteNotificationTypes respectively again. I have tried this and it works.

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

if you are using FireBase to send push notifications to your devices , you can use topic subscription to enable push notification in subscribed devices and unsubscribe users from the topic when you don't want the user to receive push notification in those devices that have been unsubscribed.

to subscribe a user to a topic simply import Firebase, then use this method:

Messaging.messaging().subscribe(toTopic: "topicName")

and to unsubscribe a user use:

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