Enable/Disable Apple Push Notification from iPhone

2019-01-11 04:12发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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");


回答4:

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.



回答5:

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.



回答6:

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



回答7:

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