Skip to content

Commit 64da9a1

Browse files
committed
Merge pull request phonegap#178 from AdriVanHoudt/always-notify
allow forceShow option to always notify (android only atm)
2 parents 60a22d9 + 4c2f224 commit 64da9a1

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Parameter | Description
7171
`options.android.sound` | `Boolean` Optional. If `true` it plays the sound specified in the push data or the default system sound. Default is `true`.
7272
`options.android.vibrate` | `Boolean` Optional. If `true` the device vibrates on receipt of notification. Default is `true`.
7373
`options.android.clearNotifications` | `Boolean` Optional. If `true` the app clears all pending notifications when it is closed. Default is `true`.
74+
`options.android.forceShow` | `Boolean` Optional. If `true` will always show a notification, even when the app is on the foreground. Default is `false`.
7475
`options.ios` | `JSON Object` iOS specific initialization options.
7576
`options.ios.alert` | `Boolean`\|`String` Optional. If `true`\|`"true"` the device shows an alert on receipt of notification. Default is `false`\|`"false"`.
7677
`options.ios.badge` | `Boolean`\|`String` Optional. If `true`\|`"true"` the device sets the badge number on receipt of notification. Default is `false`\|`"false"`.

src/android/com/adobe/phonegap/push/GCMIntentService.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,42 @@ protected void onMessage(Context context, Intent intent) {
8787
// Extract the payload from the message
8888
Bundle extras = intent.getExtras();
8989
if (extras != null) {
90-
// if we are in the foreground, just surface the payload, else post it to the statusbar
91-
if (PushPlugin.isInForeground()) {
90+
91+
SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
92+
boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);
93+
94+
// if we are in the foreground and forceShow is `false` only send data
95+
if (!forceShow && PushPlugin.isInForeground()) {
9296
extras.putBoolean(FOREGROUND, true);
9397
PushPlugin.sendExtras(extras);
9498
}
99+
// if we are in the foreground and forceShow is `true`, force show the notification if the data has at least a message or title
100+
else if (forceShow && PushPlugin.isInForeground()) {
101+
extras.putBoolean(FOREGROUND, true);
102+
103+
showNotificationIfPossible(context, extras);
104+
}
105+
// if we are not in the foreground always send notification if the data has at least a message or title
95106
else {
96107
extras.putBoolean(FOREGROUND, false);
97108

98-
// Send a notification if there is a message
99-
String message = this.getMessageText(extras);
100-
String title = getString(extras, TITLE, "");
101-
if ((message != null && message.length() != 0) ||
102-
(title != null && title.length() != 0)) {
103-
createNotification(context, extras);
104-
}
109+
showNotificationIfPossible(context, extras);
105110
}
106111
}
107112
}
113+
114+
private void showNotificationIfPossible (Context context, Bundle extras) {
115+
116+
// Send a notification if there is a message or title, otherwise just send data
117+
String message = this.getMessageText(extras);
118+
String title = getString(extras, TITLE, "");
119+
if ((message != null && message.length() != 0) ||
120+
(title != null && title.length() != 0)) {
121+
createNotification(context, extras);
122+
} else {
123+
PushPlugin.sendExtras(extras);
124+
}
125+
}
108126

109127
public void createNotification(Context context, Bundle extras) {
110128
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
@@ -210,7 +228,7 @@ public void createNotification(Context context, Bundle extras) {
210228
setNotificationCount(extras, mBuilder);
211229

212230
/*
213-
* Notication add actions
231+
* Notification add actions
214232
*/
215233
createActions(extras, mBuilder, resources, packageName);
216234

src/android/com/adobe/phonegap/push/PushConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ public interface PushConstants {
4141
public static final String COUNT = "count";
4242
public static final String FROM = "from";
4343
public static final String COLLAPSE_KEY = "collapse_key";
44+
public static final String FORCE_SHOW = "forceShow";
4445
}

src/android/com/adobe/phonegap/push/PushPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void run() {
8080
editor.putBoolean(SOUND, jo.optBoolean(SOUND, true));
8181
editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true));
8282
editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
83+
editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
8384
editor.commit();
8485
}
8586

0 commit comments

Comments
 (0)