Skip to content

Commit

Permalink
Add new setting notificationId so notifications can be uniquely ident…
Browse files Browse the repository at this point in the history
…ified and subsequent notifications will not override / overwrite previous ones

Add new method cancelByNotificationId(notificationId) to cancel a specific notification
  • Loading branch information
morinel committed May 26, 2016
1 parent ce3a70a commit 257272e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
6 changes: 4 additions & 2 deletions documentation/index.md
Expand Up @@ -4,7 +4,7 @@

A Titanium module for registering a device with Google Cloud Messaging and handling push notifications sent to the device. Both push notifications and topic subscriptions are supported.

1. Install the module as usual in Titanium Studio by downloading the [zip file](https://github.com/morinel/gcmpush/releases/download/1.4/nl.vanvianen.android.gcm-android-1.4.zip) or use ```gittio install nl.vanvianen.android.gcm```
1. Install the module as usual in Titanium Studio by downloading the [zip file](https://github.com/morinel/gcmpush/releases/download/1.5/nl.vanvianen.android.gcm-android-1.5.zip) or use ```gittio install nl.vanvianen.android.gcm```
1. Refer to the examples for possibilities.
1. Send a server push notification with your preferred server-side technology to the registrationId returned while registering your device.
1. The callback you specified will then be called.
Expand Down Expand Up @@ -45,6 +45,7 @@ public void sendPush() {
.addData("bigText", "true")
.addData("ledOn", "200")
.addData("ledOff", "300")
.addData("notificationId", "12345");
.build();
try {
/* Use the registrationIds returned in the success handler in the apps registerPush() call. */
Expand Down Expand Up @@ -85,9 +86,10 @@ See the [example](https://github.com/morinel/gcmpush/blob/master/example/app.js)
1. **ticker** (string): specify a static ticker for the notification (server data will be ignored)
1. **ledOn** (integer): the number of ms the LED should be on while flashing, see [javadoc](http://developer.android.com/reference/android/app/Notification.html#ledOnMS)
1. **ledOff** (integer): the number of ms the LED should be off while flashing, see [javadoc](http://developer.android.com/reference/android/app/Notification.html#ledOffMS)
1. **notificationId** (integer): a (unique) integer to identify the notification. If specified, subsequent notifications will not override the previous one.


The settings sound, vibrate, insistent, group, localOnly, priority and bigText can also be set as data in the push message being received (see the server-side example above).
The settings sound, vibrate, insistent, group, localOnly, priority, bigText and notificationId can also be set as data in the push message being received (see the server-side example above).

If the app is not active when the notification is received, use gcm.getLastData() to retrieve the contents of the notification and act accordingly to start or resume the app in a suitable way. If you're done, call gcm.clearLastData(), otherwise the same logic will happen when resuming the app again, see the [example](https://github.com/morinel/gcmpush/blob/master/example/app.js).

Expand Down
2 changes: 1 addition & 1 deletion manifest
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.4
version: 1.5
apiversion: 3
description: Google Cloud Push for Titanium
author: Jeroen van Vianen <jeroen@vanvianen.nl>
Expand Down
38 changes: 33 additions & 5 deletions src/nl/vanvianen/android/gcm/GCMIntentService.java
Expand Up @@ -34,8 +34,9 @@
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class GCMIntentService extends GCMBaseIntentService {

Expand All @@ -47,6 +48,8 @@ public class GCMIntentService extends GCMBaseIntentService {
private static final String DEFAULT_MESSAGE_KEY = "message";
private static final String DEFAULT_TICKER_KEY = "ticker";

private final static AtomicInteger notificationCounter = new AtomicInteger(0);

public GCMIntentService() {
super("");
}
Expand Down Expand Up @@ -137,6 +140,7 @@ protected void onMessage(Context context, Intent intent) {
boolean localOnly = true;
int priority = 0;
boolean bigText = false;
int notificationId = 1;

Integer ledOn = null;
Integer ledOff = null;
Expand Down Expand Up @@ -302,6 +306,14 @@ protected void onMessage(Context context, Intent intent) {
}
}

if (notificationSettings.get("notificationId") != null) {
if (notificationSettings.get("notificationId") instanceof Integer) {
notificationId = (Integer) notificationSettings.get("notificationId");
} else {
Log.e(LCAT, "Invalid setting notificationId, should be Integer");
}
}

} else {
Log.d(LCAT, "No notification settings found");
}
Expand Down Expand Up @@ -371,7 +383,7 @@ protected void onMessage(Context context, Intent intent) {

/* Whether notification should be for this device only or bridged to other devices, can also be set in the push notification payload */
if (data.get("localOnly") != null) {
localOnly = Boolean.getBoolean((String) data.get("localOnly"));
localOnly = Boolean.valueOf((String) data.get("localOnly"));
}
builder.setLocalOnly(localOnly);
Log.i(LCAT, "LocalOnly: " + localOnly);
Expand All @@ -389,7 +401,7 @@ protected void onMessage(Context context, Intent intent) {

/* Specify whether bigtext should be used, can also be set in the push notification payload */
if (data.get("bigText") != null) {
bigText = Boolean.getBoolean((String) data.get("bigText"));
bigText = Boolean.valueOf((String) data.get("bigText"));
}
if (bigText) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
Expand All @@ -414,7 +426,7 @@ protected void onMessage(Context context, Intent intent) {

/* Vibrate, can also be set in the push notification payload */
if (data.get("vibrate") != null) {
vibrate = Boolean.getBoolean((String) data.get("vibrate"));
vibrate = Boolean.valueOf((String) data.get("vibrate"));
}
if (vibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
Expand All @@ -430,6 +442,22 @@ protected void onMessage(Context context, Intent intent) {
}
Log.i(LCAT, "Insistent: " + insistent);

/* notificationId, set in push payload to specify multiple notifications should be shown. If not specified, subsequent notifications "override / overwrite" the older ones */
if (data.get("notificationId") != null) {
if (data.get("notificationId") instanceof Integer) {
notificationId = (Integer) data.get("notificationId");
} else if (data.get("notificationId") instanceof String) {
try {
notificationId = Integer.parseInt((String) data.get("notificationId"));
} catch (NumberFormatException ex) {
Log.e(LCAT, "Invalid setting notificationId, should be Integer");
}
} else {
Log.e(LCAT, "Invalid setting notificationId, should be Integer");
}
}
Log.i(LCAT, "Notification ID: " + notificationId);

/* Specify LED flashing */
if (ledOn != null || ledOff != null) {
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
Expand All @@ -445,7 +473,7 @@ protected void onMessage(Context context, Intent intent) {

notification.flags |= Notification.FLAG_AUTO_CANCEL;

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, notification);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification);
}

if (GCMModule.getInstance() != null) {
Expand Down
17 changes: 16 additions & 1 deletion src/nl/vanvianen/android/gcm/GCMModule.java
Expand Up @@ -17,6 +17,7 @@
package nl.vanvianen.android.gcm;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.AsyncTask;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.gms.gcm.GcmPubSub;
Expand All @@ -26,7 +27,6 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
Expand Down Expand Up @@ -238,6 +238,21 @@ public void clearLastData() {
TiApplication.getInstance().getAppProperties().removeProperty(LAST_DATA);
}

/**
* Cancel a notification by the id given in the payload.
* @param notificationId
*/
@Kroll.method
public void cancelNotificationById(int notificationId) {
try {
NotificationManager notificationManager = (NotificationManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
Log.i(LCAT, "Notification " + notificationId + " cleared successfully");
} catch (Exception ex) {
Log.e(LCAT, "Cannot cancel notification:" + notificationId + " Error: " + ex.getMessage());
}
}

@Kroll.method
@Kroll.getProperty
@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 257272e

Please sign in to comment.