Push notification issue with iOS 10

IosPush NotificationIos10

Ios Problem Overview


I've developed one application in that i've implemented push notification. Currently it's live on apple store. Upto iOS 9 push is working fine but after iOS 10 it is not working.

What is the issue with the code?

Ios Solutions


Solution 1 - Ios

For iOS 10 using xCode 8 GM.

I have resolved my issue with following steps using xCode 8 GM for iOS 10:

  1. In the targets, under Capabilities enable Push Notifications to add Push Notifications Entitlements.

  2. Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.

    #import @interface AppDelegate : UIResponder

    @end

  3. In didFinishLaunchingWithOptions method assign UIUserNotificationSettings and implement UNUserNotificationCenter delegate.

    #define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if( !error ){ [[UIApplication sharedApplication] registerForRemoteNotifications]; } }];
    }

    return YES; }

  4. Now finally implement this two delegate methods.

//============For iOS 10=============

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    //Called when a notification is delivered to a foreground app. 

    NSLog(@"Userinfo %@",notification.request.content.userInfo);

    completionHandler(UNNotificationPresentationOptionAlert);
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

   //Called to let your app know which action was selected by the user for a given notification.

   NSLog(@"Userinfo %@",response.notification.request.content.userInfo);

}

Please remain the code as it is you are using for iOS 9, Only add lines of code to support Push notification for iOS 10 using UserNotifications.framework.

Solution 2 - Ios

Everything was working fine before iOS 10, In my case only capabilities settings cause this issue.

It must be turned on for push notification.

enter image description here

Solution 3 - Ios

I had an issue with iOS 10 silent push notifications. In iOS9 and earlier, sending a push notification that had additional data fields but had an empty aps attribute in the data worked fine. But in iOS10 a push notification with an empty aps attribute does not hit the didReceiveRemoteNotification app delegate method at all, meaning that all my silent push notifications (notifications that we just use internally to trigger actions while the app is open) stopped working in iOS10.

I was able to fix this without pushing an update to my app by adding at least one attribute to the aps part of the push notification, in my case I just added badge: 0 and my silent push notifications started working again in iOS 10. I hope this helps someone else!

Solution 4 - Ios

The swift 3 version of @Ashish Shah code is:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
//notifications
        if #available(iOS 10.0, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                if error == nil{
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        } else {
            // Fallback on earlier versions
        }
                
        return true
    }
    @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) {
        
    }

Solution 5 - Ios

Don't forget, when testing, you must use sandbox address for your notifications to work.

Solution 6 - Ios

On iOS, the application approaches the client for authorization to get push warnings by calling the registerUserNotificationSettings: strategy for UIApplication.

The application calls the registerForRemoteNotifications: technique for UIApplication (iOS) or the strategy registerForRemoteNotificationTypes: of NSApplication (OS X).

The application executes the application:didRegisterForRemoteNotificationsWithDeviceToken: technique for UIApplicationDelegate (iOS) or NSApplicationDelegate (OS X) to get the one of a kind gadget token produced by the push benefit.

The application executes the application:didFailToRegisterForRemoteNotificationsWithError: technique for UIApplicationDelegate (iOS) or NSApplicationDelegate (OS X) to get a blunder if the enrolment fizzled.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohsin SabasaraView Question on Stackoverflow
Solution 1 - IosAshish ShahView Answer on Stackoverflow
Solution 2 - IosAiOsNView Answer on Stackoverflow
Solution 3 - IosjakeduncView Answer on Stackoverflow
Solution 4 - IosLuchoView Answer on Stackoverflow
Solution 5 - IosAshkan GhodratView Answer on Stackoverflow
Solution 6 - IosanjalihiaimView Answer on Stackoverflow