From dfcb3aed18ad5dbcebb3d9d9c712702691e8092d Mon Sep 17 00:00:00 2001 From: Simon MacDonald Date: Mon, 7 Dec 2015 17:53:00 -0500 Subject: [PATCH] Issue #293: Force notification on Android to be background if content-available: 1 --- docs/PAYLOAD.md | 17 +++++++++-------- .../adobe/phonegap/push/GCMIntentService.java | 6 +++++- .../com/adobe/phonegap/push/PushConstants.java | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/PAYLOAD.md b/docs/PAYLOAD.md index c9327872d..6d68ce04f 100644 --- a/docs/PAYLOAD.md +++ b/docs/PAYLOAD.md @@ -289,27 +289,28 @@ This will produce the following notification in your tray: On Android if you want your `on('notification')` event handler to be called when your app is in the background it is relatively simple. -The JSON you send to GCM should not contain a title or message parameter. For instance the following JSON: +First the JSON you send from GCM will need to include `"content-available": "1"`. This will tell the push plugin to call your `on('notification')` event handler no matter what other data is in the push notification. ```javascript { title: "Test Push", message: "Push number 1", - info: "super secret info" + info: "super secret info", + content-available: "1" } ``` -will produce a notification in the notification shade and call your `on('notification')` event handler. +or -However if you want your `on('notification')` event handler called but no notification to be shown in the shader you would omit the `alert` property and send the following JSON to GCM: ```javascript { - info: "super secret info" + info: "super secret info", + content-available: "1" } ``` -Omitting the message and title properties will keep your push from being added to the notification shade but it will still trigger your `on('notification')` event handler. +If do not want this type of behaviour just omit `"content-available": 1` from your push data and your `on('notification')` event handler will not be called. # iOS Behaviour @@ -341,7 +342,7 @@ For instance the following JSON: { aps: { alert: "Test background push", - "content-available": 1 + content-available: 1 } } ``` @@ -355,7 +356,7 @@ However if you want your `on('notification')` event handler called but no notifi aps: { data: "Test silent background push", moredata: "Do more stuff", - "content-available": 1 + content-available: 1 } } ``` diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index f0a090753..214393eaf 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -196,9 +196,11 @@ private void showNotificationIfPossible (Context context, Bundle extras) { // Send a notification if there is a message or title, otherwise just send data String message = extras.getString(MESSAGE); String title = extras.getString(TITLE); + String contentAvailable = extras.getString(CONTENT_AVAILABLE); Log.d(LOG_TAG, "message =[" + message + "]"); Log.d(LOG_TAG, "title =[" + title + "]"); + Log.d(LOG_TAG, "contentAvailable =[" + contentAvailable + "]"); if ((message != null && message.length() != 0) || (title != null && title.length() != 0)) { @@ -206,7 +208,9 @@ private void showNotificationIfPossible (Context context, Bundle extras) { Log.d(LOG_TAG, "create notification"); createNotification(context, extras); - } else { + } + + if ("1".equals(contentAvailable)) { Log.d(LOG_TAG, "send notification event"); PushPlugin.sendExtras(extras); } diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index e466331a2..45f16d48b 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -51,4 +51,5 @@ public interface PushConstants { public static final String COLLAPSE_KEY = "collapse_key"; public static final String FORCE_SHOW = "forceShow"; public static final String GCM = "GCM"; + public static final String CONTENT_AVAILABLE = "content-available"; }