prabakaran iOS prabakaran iOS - 3 months ago 46
iOS Question

Enable/Disable push notification after did finish launch or inside our app

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.

Answer

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]];
}