CloudKit Push Notification, App running on background

我的未来我决定 提交于 2019-12-07 09:48:27

问题


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);
}

回答1:


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

See this screenshot:

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.




回答2:


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.



来源:https://stackoverflow.com/questions/29950554/cloudkit-push-notification-app-running-on-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!