4

When iOS8.2-app is running in the background, it does not receive any push notification,

while if it is running on the foreground, it receives the push notifications fine.

Any idea what is going on ?

Running on CloudKit Development mode, the subscription is for add,edit,and remove, and using the following didReceiveRemoteNotification:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"Push received.");

    NSDictionary * apsDict = [userInfo objectForKey:@"aps"];

    NSString * alertText = [apsDict objectForKey:@"alert"];

    //TODO: get the record information from the notification and create the appropriate message string.

    if(application.applicationState == UIApplicationStateActive) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:alertText delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    } else {

        if([application currentUserNotificationSettings].types & UIUserNotificationTypeAlert) {
            UILocalNotification * localNotification = [[UILocalNotification alloc] init];
            localNotification.alertBody = NSLocalizedString(@"alert body", nil);;
            localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
            [application presentLocalNotificationNow:localNotification];
        }
    }

    completionHandler(UIBackgroundFetchResultNoData);
}
John
  • 8,468
  • 5
  • 36
  • 61
Lydon Ch
  • 8,637
  • 20
  • 79
  • 132

2 Answers2

4

When you go to your app settings capabilities, do you have remote notifications enabled for background modes?

See this screenshot: enter image description here

Besides that, did you register for all notification types like this:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil))
    application.registerForRemoteNotifications()

And in your subscription, do you send an alertBody or alertLocalizationKey for the CKNotificationInfo? If you do that, then you will already get a notification from the OS and you don't need to setup a local notification.

Update: As Porton mentioned below, this issue was solved by filling in the alertBody.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Hi @Edwin yes Background fetch and Remote notifications are ON. Anything else that is obvious that might be missed ? – Lydon Ch Apr 30 '15 at 13:19
  • In your subscription, do you send an alertBody or alertLocalizationKey for the CKNotificationInfo? Then you will already get a notification from the OS and you don't need to setup a local notification. But then still the notifications should have arrived. Especially when you say they arrive when the app is active. – Edwin Vermeer Apr 30 '15 at 13:35
  • Did you register for all types? see additional comment in awnser – Edwin Vermeer Apr 30 '15 at 13:38
  • 3
    alertBody was an empty string !!! Once alertBody is set to a non-empty string, remote notifications came through (as banner as per userNotificationSettings). Thanks a lot @Edwin. I marked your answer as the solution, would you kindly update your answer to include alertBody ? – Lydon Ch Apr 30 '15 at 17:00
  • I have added that to the answer – Edwin Vermeer Apr 30 '15 at 18:52
  • Having a non-empty alertBody causes my didReceiveRemoteNotification to be executed. However, I would like to use silent notifications (without an alert being shown). How can I do this? – John Jul 15 '15 at 00:02
  • Set "shouldSendContentAvailable" on CKSubscriptionInfo to YES, then you can clear the alertBody and receive silent notifications. NOTE: I don't think you need to enable "Remote Notifications" in Background Modes, as described above, for silent notifications. – Peter Johnson Dec 16 '15 at 12:37
4

I had the same problem and have spent several hours on making push notifications work when the app is not in the foreground.

Originally, I did not set an alertBody since I did not want the user to see a message when CloudKit data has changed.

However, as stated above, without an alertBody, the method didReceiveRemoteNotification is not being called when the app is not in the foreground.

I finally tried to set alertBodyto be an empty string:

CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertBody = @"";

Now it works: didReceiveRemoteNotification is called even when the app is not active AND no alert message is shown to the user.

John
  • 8,468
  • 5
  • 36
  • 61