2

I need to enable/disable or register/unregister push notification inside our app after did finish launch. I'll do this function after launch and in my app "account setting"

I have try below code but it's not work.

 if (cell.switchButton.on) {
    if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}
else {
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}

My requirement: I'll Enable or Disable notification toggle inside my app programmatically instead of directly go to setting -> Notification.

prabakaran iOS
  • 681
  • 7
  • 19

3 Answers3

3

You should not use unregisterForRemoteNotifications, it will unregister APNS completely as apple said

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app.

here is link

It will be better if you implement disabling push notifications on your server side. Just told your server guy to make service to not send APNs.

or you can also open notifications section of the settings app as below

if (&UIApplicationOpenSettingsURLString != NULL){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
ajay_nasa
  • 2,278
  • 2
  • 28
  • 45
  • 1
    is possible enable and disable notification toggle programmatically. I mean i'll not go to setting and disable or enable toggle. instead of i can switch toggle programatically inside my app. – prabakaran iOS Sep 09 '16 at 09:03
  • for swift --> if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) { UIApplication.shared.openURL(appSettings as URL) } – Rajni Gujarati Feb 04 '21 at 10:05
1

You can easily enable and disable push notifications in your application by calling registerForRemoteNotificationTypes and unregisterForRemoteNotificationTypes respectively.

To Enable

[[UIApplication sharedApplication] registerForRemoteNotifications];

To Disable

[[UIApplication sharedApplication] unregisterForRemoteNotifications];
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
1

What exactly is not working?

Do you get a device token (app delegate's application:didRegisterForRemoteNotificationsWithDeviceToken:) or an error (application:didFailToRegisterForRemoteNotificationsWithError:)?

As an alternate solution, you could store the user's preference (receiving notifications or not) on your server instead of subscribing and unsubscribing.

tilo
  • 14,009
  • 6
  • 68
  • 85