Skip to content

Commit

Permalink
Issue phonegap#293: Force notification on Android to be background if…
Browse files Browse the repository at this point in the history
… content-available: 1
  • Loading branch information
macdonst authored and sud80 committed Jan 29, 2016
1 parent e0c4697 commit dfcb3ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
17 changes: 9 additions & 8 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -341,7 +342,7 @@ For instance the following JSON:
{
aps: {
alert: "Test background push",
"content-available": 1
content-available: 1
}
}
```
Expand All @@ -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
}
}
```
Expand Down
6 changes: 5 additions & 1 deletion src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,21 @@ 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)) {

Log.d(LOG_TAG, "create notification");

createNotification(context, extras);
} else {
}

if ("1".equals(contentAvailable)) {
Log.d(LOG_TAG, "send notification event");
PushPlugin.sendExtras(extras);
}
Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit dfcb3ae

Please sign in to comment.