From ed8360ca33056b7ce91ade389412d89338ae0fa6 Mon Sep 17 00:00:00 2001 From: Simon MacDonald Date: Thu, 4 Feb 2016 15:08:38 -0500 Subject: [PATCH] Issue #551: Handling notifications in the background (without loading the app UI) --- docs/PAYLOAD.md | 8 ++--- plugin.xml | 2 ++ .../push/BackgroundActionButtonHandler.java | 32 +++++++++++++++++++ .../adobe/phonegap/push/GCMIntentService.java | 22 ++++++++++--- .../phonegap/push/PushHandlerActivity.java | 20 ++++++++++-- 5 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java diff --git a/docs/PAYLOAD.md b/docs/PAYLOAD.md index 22adb0210..de1be81c5 100644 --- a/docs/PAYLOAD.md +++ b/docs/PAYLOAD.md @@ -430,9 +430,9 @@ Your notification can include action buttons. If you wish to include an icon alo "data": { "title": "AUX Scrum", "message": "Scrum: Daily touchbase @ 10am Please be on time so we can cover everything on the agenda.", - "actions": [ - { "icon": "emailGuests", "title": "EMAIL GUESTS", "callback": "app.emailGuests"}, - { "icon": "snooze", "title": "SNOOZE", "callback": "app.snooze"}, + "actions": [ + { "icon": "emailGuests", "title": "EMAIL GUESTS", "callback": "app.emailGuests", "foreground": true}, + { "icon": "snooze", "title": "SNOOZE", "callback": "app.snooze", "foreground": false}, ] } } @@ -463,7 +463,7 @@ This will produce the following notification in your tray: ![action_combo](https://cloud.githubusercontent.com/assets/353180/9313435/02554d2a-44f1-11e5-8cd9-0aadd1e02b18.png) -If your users clicks on the main body of the notification your app will be opened. However if they click on either of the action buttons the app will open (or start) and the specified JavaScript callback will be executed. In this case it is `app.emailGuests` and `app.snooze` respectively. +If your users clicks on the main body of the notification your app will be opened. However if they click on either of the action buttons the app will open (or start) and the specified JavaScript callback will be executed. In this case it is `app.emailGuests` and `app.snooze` respectively. If you set the `foreground` property to `true` the app will be brought to the front, if `foreground` is `false` then the callback is run without the app being brought to the foreground. ## Led in Notifications diff --git a/plugin.xml b/plugin.xml index 579683a0f..cbb38f901 100755 --- a/plugin.xml +++ b/plugin.xml @@ -53,6 +53,7 @@ + + diff --git a/src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java b/src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java new file mode 100644 index 000000000..f8642e33a --- /dev/null +++ b/src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java @@ -0,0 +1,32 @@ +package com.adobe.phonegap.push; + +import android.app.NotificationManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +public class BackgroundActionButtonHandler extends BroadcastReceiver implements PushConstants { + private static String LOG_TAG = "PushPlugin_BackgroundActionButtonHandler"; + + @Override + public void onReceive(Context context, Intent intent) { + Bundle extras = intent.getExtras(); + Log.d(LOG_TAG, "BackgroundActionButtonHandler = " + extras); + + int notId = intent.getIntExtra(NOT_ID, 0); + Log.d(LOG_TAG, "not id = " + notId); + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(GCMIntentService.getAppName(context), notId); + + if (extras != null) { + Bundle originalExtras = extras.getBundle(PUSH_BUNDLE); + + originalExtras.putBoolean(FOREGROUND, false); + originalExtras.putBoolean(COLDSTART, false); + originalExtras.putString(CALLBACK, extras.getString("callback")); + PushPlugin.sendExtras(originalExtras); + } + } +} diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index 214393eaf..b55bede7a 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -337,10 +337,22 @@ private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, R Log.d(LOG_TAG, "adding action"); JSONObject action = actionsArray.getJSONObject(i); Log.d(LOG_TAG, "adding callback = " + action.getString(CALLBACK)); - Intent intent = new Intent(this, PushHandlerActivity.class); - intent.putExtra(CALLBACK, action.getString(CALLBACK)); - intent.putExtra(PUSH_BUNDLE, extras); - PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT); + boolean foreground = action.getBoolean(FOREGROUND); + Intent intent = null; + PendingIntent pIntent = null; + if (foreground) { + intent = new Intent(this, PushHandlerActivity.class); + intent.putExtra(CALLBACK, action.getString(CALLBACK)); + intent.putExtra(PUSH_BUNDLE, extras); + intent.putExtra(FOREGROUND, foreground); + pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } else { + intent = new Intent(this, BackgroundActionButtonHandler.class); + intent.putExtra(CALLBACK, action.getString(CALLBACK)); + intent.putExtra(PUSH_BUNDLE, extras); + intent.putExtra(FOREGROUND, foreground); + pIntent = PendingIntent.getBroadcast(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } mBuilder.addAction(resources.getIdentifier(action.getString(ICON), DRAWABLE, packageName), action.getString(TITLE), pIntent); @@ -586,7 +598,7 @@ public Bitmap getBitmapFromURL(String strURL) { } } - private static String getAppName(Context context) { + public static String getAppName(Context context) { CharSequence appName = context.getPackageManager().getApplicationLabel(context.getApplicationInfo()); return (String)appName; } diff --git a/src/android/com/adobe/phonegap/push/PushHandlerActivity.java b/src/android/com/adobe/phonegap/push/PushHandlerActivity.java index dd9fbd36b..6a490dee9 100644 --- a/src/android/com/adobe/phonegap/push/PushHandlerActivity.java +++ b/src/android/com/adobe/phonegap/push/PushHandlerActivity.java @@ -20,17 +20,31 @@ public class PushHandlerActivity extends Activity implements PushConstants { @Override public void onCreate(Bundle savedInstanceState) { GCMIntentService gcm = new GCMIntentService(); - gcm.setNotification(getIntent().getIntExtra(NOT_ID, 0), ""); + int notId = getIntent().getIntExtra(NOT_ID, 0); + Log.d(LOG_TAG, "not id = " + notId); + gcm.setNotification(notId, ""); + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(GCMIntentService.getAppName(this), notId); super.onCreate(savedInstanceState); Log.v(LOG_TAG, "onCreate"); + String callback = getIntent().getExtras().getString("callback"); + Log.d(LOG_TAG, "callback = " + callback); + boolean foreground = getIntent().getExtras().getBoolean("foreground", true); + + Log.d(LOG_TAG, "bringToForeground = " + foreground); boolean isPushPluginActive = PushPlugin.isActive(); processPushBundle(isPushPluginActive); finish(); - if (!isPushPluginActive) { + Log.d(LOG_TAG, "isPushPluginActive = " + isPushPluginActive); + + if (!isPushPluginActive && foreground) { + Log.d(LOG_TAG, "forceMainActivityReload"); forceMainActivityReload(); + } else { + Log.d(LOG_TAG, "don't want main activity"); } } @@ -67,4 +81,4 @@ protected void onResume() { final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancelAll(); } -} \ No newline at end of file +}