Enable/Disable Apple Push Notification from iPhone app?
Asked Answered
H

7

21

I have one more doubt on APNS. That is when the app first launch the app asks for Apple Push Notification Permission if the user accepted the they can receive the notifications. If the user cancelled they can't receive any notifications. Am I clear??

Now my doubt is,

  1. At first time if the user cancelled the push notification service from the app (Clicked Cancel button) again after some days if they want receive Apple Push Notification it is possible to enable the Apple Push Notification again for the particular user from the App.

  2. And if the user accept the apple push notification service first and after some days if they don't want to receive the notifications it is possible to disable the APNS in our app? I hope you understand my doubt. Can any one please clarify this doubt?

  3. It is possible to do these above scenarios in our iPhone app?

Please help me. Thanks in advance.

Hollinger answered 8/5, 2012 at 6:49 Comment(0)
D
15

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.

Denim answered 8/5, 2012 at 6:54 Comment(2)
Thanks for your answer Adig. Is there any possibility to find by the code that the user switched off to receive the notifications from my app (From our iPhone device settings screen). Thanks in advance.Hollinger
You can check if any type of push notifications are enabled by using this : if([UIApplication sharedApplication].enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)Denim
M
16

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.

Manxman answered 8/5, 2012 at 7:14 Comment(0)
D
15

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.

Denim answered 8/5, 2012 at 6:54 Comment(2)
Thanks for your answer Adig. Is there any possibility to find by the code that the user switched off to receive the notifications from my app (From our iPhone device settings screen). Thanks in advance.Hollinger
You can check if any type of push notifications are enabled by using this : if([UIApplication sharedApplication].enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)Denim
K
5

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");
Kilmarx answered 27/6, 2014 at 11:58 Comment(1)
As mentioned above, enabling can only happen once.Naman
S
1

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.

Sorely answered 8/5, 2012 at 6:58 Comment(2)
Thanks for your answer Malek_jundi. Is there any possibility to find by the code that the user switched off to receive the notifications from my app (From our iPhone device settings screen). Thanks in advance.Hollinger
I don't think so .. but its simple you will make an api function hold 1 parameter (device toke) once the user turned off the notification (its up to you how to make it in your app but the UISwitch it the best choice) you will call this api and set a flag for this token to not receive notification .. thats it.Sorely
P
0

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.

Puffery answered 29/8, 2012 at 9:10 Comment(0)
M
0

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"?

Monophthong answered 17/12, 2015 at 8:48 Comment(0)
P
0

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()
Peonage answered 24/6, 2017 at 5:48 Comment(3)
so that means , I can put one ON/OFF switch in my app for my App Notification & I can enable/disable it at any time I want .. does Apple approve this ?Trela
@shaqirsaiyed Yet I've not confirmed it, as I've been engaged with enterprise app only. But surely I'll check this with apple. This can be very useful.Peonage
yea please check it if its possible.Trela

© 2022 - 2024 — McMap. All rights reserved.