Why a device does not receive push notification on background?

180 Views Asked by At

On iOS10 when an app is opened I'm receiving Push Notifications, but when I press home button and app is on background mode - it does not receive anything.

My code is:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 10.0, *) {
        let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound]
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Sound, .Alert, .Badge], completionHandler: { (granted: Bool, error: NSError?) in

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.currentNotificationCenter().delegate = self
            // For iOS 10 data message (sent via FCM)
            FIRMessaging.messaging().remoteMessageDelegate = self
        })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

// Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()

    application.registerForRemoteNotifications()

}
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
    print("User Info => ",response.notification.request.content.userInfo)
    completionHandler()
}

So I cannot find where the problem is? Any idea? Or there is something new in iOS10?

I already turned on these features:

enter image description here enter image description here

2

There are 2 best solutions below

4
On

Have you tried doing this

enter image description here and this

enter image description here

Please confirm after doing these, i will edit this answer accordingly

0
On

Try with this steps for iOS 10,

  • First of all In the targets -> Capabilities -> enable Push Notifications to add Push Notifications Entitlements.

  • Second one Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.

    import < UserNotifications/UserNotifications.h >

    @interface AppDelegate : UIResponder < UIApplicationDelegate, UNUserNotificationCenterDelegate >

    @end

  • Third step - Inside 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;
    }
    
    • Last step - 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);

}

I hope it will helps you.

Thanks.