Enable or Disable Iphone Push Notifications inside the app
Asked Answered
B

5

29

I have a iphone app which is enable to receive push notifications. Currently i can disable push notifications for my app by going to the iphone settings/Notifications.

But i want to add a switch or button inside my app to enable or disable push notifications.

It can be done because i saw it in foursqure iphone app did it. They got a section in settings call notification settings and user can enable or disable different kind of notification for the app.

I look all over the net to find a proper solution for this but still not found a way. Can any one please give any idea how to do that ?

Thanks in advance :)

Broomfield answered 9/5, 2012 at 5:59 Comment(2)
Is this allowed by Apple?Gynaeco
@Gynaeco me too looking for same , but still not got any clear answer on this.Ezekielezell
F
24

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..

Faiyum answered 9/5, 2012 at 6:3 Comment(1)
As the answer below states, calling unregisterForRemoteNotificationTypes will halt delivery of push notifications - I have tested this on my app on user logout and it does work.Consternation
E
35

[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.

Ectomy answered 25/6, 2012 at 10:10 Comment(3)
Is this allowed by Apple?Gynaeco
This still is subject to the Notification Center settings though, right?Mor
As @TiagoAlmeida said, it is not longer working on iOS10 and also not recommended by Apple. They say in their docs that unregisterForRemoteNotificationTypes should be only used in rare occasions, like totally disable notifications for your appGlycerite
F
24

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..

Faiyum answered 9/5, 2012 at 6:3 Comment(1)
As the answer below states, calling unregisterForRemoteNotificationTypes will halt delivery of push notifications - I have tested this on my app on user logout and it does work.Consternation
I
2

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()
Inflow answered 20/7, 2017 at 14:12 Comment(1)
so you mean to say you have one Enable/Disable button inside your App's settings and you are enabling and disabling notifications using this code, right ? is it working ? does apple approve this ?Ezekielezell
T
0

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")
Turbo answered 10/3, 2019 at 19:12 Comment(0)
D
0

Check the permission first and then change it accordingly.

func checkPermission(completion: @escaping (_ isCameraPermissionOn: Bool) -> ()) {
        let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: { permission in
            switch permission.authorizationStatus  {
            case .authorized:
                //If user allow the permission
                completion(true)
            case .denied:
                //If user denied the permission
                completion(false)
            case .notDetermined:
                //First time
                current.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                    if granted {
                        completion(true)
                    } else {
                        completion(false)
                    }
                }
            case .provisional:
                // @available(iOS 12.0, *)
                // The application is authorized to post non-interruptive user notifications.
                completion(true)
            case .ephemeral:
                // @available(iOS 14.0, *)
                // The application is temporarily authorized to post notifications. Only available to app clips.
                completion(true)
            @unknown default:
                print("Unknow Status")
            }
        })
    }

Call:

AppsPermissionCheckingManager.shared.checkPermission { isPermissionOn in
            DispatchQueue.main.async {
                if isPermissionOn == true {
                    //It's on
                } else {
                    //It's off
                }
            }
        }
Dilatometer answered 4/1, 2022 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.