Push notification issue with iOS 10

48,287

Solution 1

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 <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@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

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

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

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

    }
Share:
48,287
Mohsin Sabasara
Author by

Mohsin Sabasara

Updated on April 30, 2020

Comments

  • Mohsin Sabasara
    Mohsin Sabasara about 4 years

    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?

  • Jayaprakash
    Jayaprakash almost 8 years
    Hi Ashish, Thanks. It is a good tutorial. But I wonder this has to be explicitly mentioned by Apple right? Do they have any official documentation stating this?
  • Himanth
    Himanth almost 8 years
    I did the same but not able to receive notification when app was terminated
  • Mr.Fingers
    Mr.Fingers almost 8 years
    -willPresentNotification == called when app is foreground and push comes, it can be used to configure to show or not to show incomed push -didReceiveNotificationResponse == called when notification opened from notification centre
  • Hyder
    Hyder almost 8 years
    SAVED MY LIFE! THANK YOU
  • samir105
    samir105 over 7 years
    I fixed it by clicking "Fix me" button in the Push notifications section. The issue was missing entitlement for push notification
  • Mohsin Sabasara
    Mohsin Sabasara over 7 years
    aps = { badge = 7; "content-available" = 1; priority = 5; };
  • Lokesh Patel
    Lokesh Patel over 7 years
    Thanks @Ashish Nice Tutorial (y)
  • Murat Yasar
    Murat Yasar over 7 years
    I was having the exact same issue. It helped me @jakedunc, thank you for sharing this.
  • Alex Napitupulu
    Alex Napitupulu over 7 years
    If you would only like to do bare minimum, you just have to call [UNUserNotificationCentre requestAuthorizationWithOptions]. Linking to UserNotification.framework and UNUserNotificationCenterDelegate are for implementing the new iOS 10 interactive push notifications.
  • Lane Rettig
    Lane Rettig over 7 years
    In my case, I only needed to do step #1. None of the others, including [UNUserNotificationCentre requestAuthorizationWithOptions], was required and push continued to work as before.
  • Eagle11
    Eagle11 over 7 years
    Same issue. It seems like iOS 10 is a bit more strict about what it allows through.
  • S.L. Barth
    S.L. Barth over 7 years
    Welcome to Stack Overflow! Please note that overt self-promotion is not allowed. I have edited out the link to your site. It is allowed to link one's own site on one's user page. It will not be indexed by search engines until you have earned a minimum amount of points, however, to prevent abuse.
  • Iyyappan Ravi
    Iyyappan Ravi over 7 years
    Hi @Ashish, i have implement above framework and code, but ios 10 not receive the notification why i don't know please help me.
  • karthikeyan
    karthikeyan over 7 years
    i want to store pay load into core data, when application in background, i did this iOS 9 and previous but iOS 10 does not working with above delegates, its only call when user taps..any idea to save push notification while in app background
  • TharakaNirmana
    TharakaNirmana over 7 years
    have you found the apple documentation related to this?
  • leolobato
    leolobato over 7 years
    You don't need Step 4 (conform to UNUserNotificationCenterDelegate) if you don't set the delegate property on UNUserNotificationCenter. It will continue behaving like iOS 9 in that case and the UIApplicationDelegate methods will be called (application:didReceiveRemoteNotification: and so on).
  • Iulian Onofrei
    Iulian Onofrei over 7 years
    Be aware that by using priority 5 may lead to pushes not showing up at all, as it can be seen in the documentation: "They are throttled, and in some cases are not delivered."!
  • Uma Madhavi
    Uma Madhavi over 7 years
    @jakedunc. aps attribute is from server side or we can it.
  • mfaani
    mfaani over 7 years
    everybody loves an answer where all you have to do is click a button :D
  • Boris Nikolic
    Boris Nikolic over 7 years
    Can you please explain this?
  • Ashkan Ghodrat
    Ashkan Ghodrat over 7 years
    In my case what happened was that when updating from ios 9 to 10, my application server was sending the notification to production server where i was in development phase, so changing it back to development sandbox address fixed the issue
  • dollar2048
    dollar2048 over 6 years
    Why don't you check granted? In case of (granted == NO) callback sends nil for the error. It means user is not registered.
  • ArgaPK
    ArgaPK about 6 years
    @AlexNapitupulu could you please tell which method will execute when the app is in background and receives a notificaation?
  • ArgaPK
    ArgaPK about 6 years
    @Honey could you please tell me which method gets executed when the app is in background and receive user notification?
  • mfaani
    mfaani about 6 years
    @ArgaPK see this answer