In my app I want to enable/disable push notification from the settings page of my app itself.Can any one suggest me a solution to turn on/off the status of app in notification center from the app ?
            Asked
            
        
        
            Active
            
        
            Viewed 4.0k times
        
    27
            
            
        2 Answers
63
            you can register and unregister the remote notification with bellow code.
Register RemoteNotification with bellow code..means Enable notification
//-- Set Notification
if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // For iOS 8 and above
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    // For iOS < 8
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
and Disable it with bellow code.
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
 
    
    
        Paras Joshi
        
- 20,427
- 11
- 57
- 70
- 
                    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20920/discussion-between-jeff-and-paras-joshi) – Jeff Dec 11 '12 at 07:05
- 
                    2@Muzammil - Here is an updated answer till iOS 11 in Swift - https://stackoverflow.com/a/44672823/5638630 – Krunal Jan 05 '18 at 11:37
14
            
            
        Swift 4
Enable Push Notification (Setup from app):
if #available(iOS 10.0, *) {
   // SETUP FOR NOTIFICATION 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 {
   // SETUP FOR NOTIFICATION FOR iOS < 10.0
   let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
   UIApplication.shared.registerUserNotificationSettings(settings)
   // This is an asynchronous method to retrieve a Device Token
   // Callbacks are in AppDelegate.swift
   // Success = didRegisterForRemoteNotificationsWithDeviceToken
   // Fail = didFailToRegisterForRemoteNotificationsWithError
    UIApplication.shared.registerForRemoteNotifications()
}
Delegate methods to handle push notifications
@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) {
    // ...register device token with our Time Entry API server via REST
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    //print("DidFaildRegistration : Device token for push notifications: FAIL -- ")
    //print(error.localizedDescription)
}
Disable Push Notifiacation:
UIApplication.shared.unregisterForRemoteNotifications()
- A document by Apple on Push Notification
 
    
    
        Krunal
        
- 77,632
- 48
- 245
- 261
