1

I've tried various combinations of the following:

  • An empty alert passed in the aps payload
  • An empty sound passed in the aps payload
  • content_available: true
  • content-available: 1

Priority is set to high. I have background fetch and remote notifications enabled in background modes in my target capabilities.

This is what my payload looks like:

JsonObject jGcmData = new JsonObject();
jGcmData.addProperty("to", "/topics/global");
jGcmData.addProperty("priority", "high");

JsonObject jData = new JsonObject();
jData.addProperty("id", nObj.id);
jData.addProperty("title", nObj.title);
jData.addProperty("body", nObj.body);

JsonObject apsData = new JsonObject();
apsData.addProperty("content-available", "1");
apsData.addProperty("alert", "");

jGcmData.add("aps", apsData);
jGcmData.add("data", jData);

The notification comes in as soon as I open the app but not in the background.

It looks like this:

[AnyHashable("body"): NTCIP Example Unit: now showing WALNUT TREE (Tue Nov 15 14:01:30 EST 2016), AnyHashable("id"): idhere, AnyHashable("collapse_key"): do_not_collapse, AnyHashable("from"): /topics/global, AnyHashable("title"): Notification of message change]

My AppDelegate functions for the foreground and background notifications when received:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler handler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("background notification received: \(userInfo)")
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: messageKey), object: nil, userInfo: userInfo)
        handler(UIBackgroundFetchResult.noData);
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        print("foreground notification received: \(userInfo)")
        GCMService.sharedInstance().appDidReceiveMessage(userInfo);
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: messageKey), object: nil, userInfo: userInfo)
    }

I never see application(didReceiveRemoteNotification:::fetchCompletionHandler) called.

Please help!

Casey Murray
  • 1,582
  • 17
  • 21
  • @Essenceofchickens I get the foreground notifications with no issue on my iOS device, on which the app is still in development and the notifications use the development certificate. Both foreground and background notifications come in on my Android device, on which the app is in production. I'm just not getting background notifications on my iOS device. – Casey Murray Nov 15 '16 at 19:41

1 Answers1

0

Turns out I need jGcmData.add("notification", jData); instead of jGcmData.add("data", jData); for APNS to process the background notification. Making that change I now see application(didReceiveRemoteNotification:::fetchCompletionHandler) being called and aside from changing how I process the payload I believe my background notification problem is resolved!

(Link to the SO question that helped me find this resolution will be added shortly!)

To clarify: GCM accepts the payload description as either notification or data APNS expects it to be notification for the background notification

Casey Murray
  • 1,582
  • 17
  • 21