Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #551: Handling notifications in the background (without loading…
Browse files Browse the repository at this point in the history
… the app UI)
  • Loading branch information
macdonst committed Feb 5, 2016
1 parent 231a816 commit ed8360c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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},
]
}
}
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="com.adobe.phonegap.push.PushHandlerActivity" android:exported="true"/>
<receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler"/>

<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
Expand Down Expand Up @@ -94,6 +95,7 @@
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/RegistrationIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PermissionUtils.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java" target-dir="src/com/adobe/phonegap/push/" />

</platform>

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
22 changes: 17 additions & 5 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

This comment has been minimized.

Copy link
@fredgalvao

fredgalvao Feb 8, 2016

Collaborator

@macdonst If foreground is absent from the JSON object, it'll run as if the deafult value for it was false, won't it? If so, it goes against the documented default value of true.

This comment has been minimized.

Copy link
@fredgalvao

fredgalvao Feb 8, 2016

Collaborator

Nevermind, I forgot to see the whole commit before writing stuff.

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);
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 17 additions & 3 deletions src/android/com/adobe/phonegap/push/PushHandlerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -67,4 +81,4 @@ protected void onResume() {
final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
}
}

0 comments on commit ed8360c

Please sign in to comment.