Skip to content

Commit 728e975

Browse files
author
Amir Gamil
committed
Allow for multiple notifications in Android
This allows for multiple notifications to be displayed on Android. Simply add data with key "notId" and an integer value when sending via GCM. Differentiate between different notifications by setting different notIds. Defaults to 0 if not key supplied.
1 parent 258e32a commit 728e975

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/android/com/plugin/gcm/GCMIntentService.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.plugin.gcm;
22

3-
import java.util.List;
4-
5-
import com.google.android.gcm.GCMBaseIntentService;
63
import org.json.JSONException;
74
import org.json.JSONObject;
85

96
import android.annotation.SuppressLint;
10-
import android.app.ActivityManager;
11-
import android.app.ActivityManager.RunningTaskInfo;
127
import android.app.Notification;
138
import android.app.NotificationManager;
149
import android.app.PendingIntent;
@@ -18,10 +13,11 @@
1813
import android.support.v4.app.NotificationCompat;
1914
import android.util.Log;
2015

16+
import com.google.android.gcm.GCMBaseIntentService;
17+
2118
@SuppressLint("NewApi")
2219
public class GCMIntentService extends GCMBaseIntentService {
2320

24-
public static final int NOTIFICATION_ID = 237;
2521
private static final String TAG = "GCMIntentService";
2622

2723
public GCMIntentService() {
@@ -94,7 +90,8 @@ public void createNotification(Context context, Bundle extras)
9490
.setWhen(System.currentTimeMillis())
9591
.setContentTitle(extras.getString("title"))
9692
.setTicker(extras.getString("title"))
97-
.setContentIntent(contentIntent);
93+
.setContentIntent(contentIntent)
94+
.setAutoCancel(true);
9895

9996
String message = extras.getString("message");
10097
if (message != null) {
@@ -108,13 +105,19 @@ public void createNotification(Context context, Bundle extras)
108105
mBuilder.setNumber(Integer.parseInt(msgcnt));
109106
}
110107

111-
mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
112-
}
113-
114-
public static void cancelNotification(Context context)
115-
{
116-
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
117-
mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);
108+
int notId = 0;
109+
110+
try {
111+
notId = Integer.parseInt(extras.getString("notId"));
112+
}
113+
catch(NumberFormatException e) {
114+
Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
115+
}
116+
catch(Exception e) {
117+
Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
118+
}
119+
120+
mNotificationManager.notify((String) appName, notId, mBuilder.build());
118121
}
119122

120123
private static String getAppName(Context context)

src/android/com/plugin/gcm/PushHandlerActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public void onCreate(Bundle savedInstanceState)
2525
boolean isPushPluginActive = PushPlugin.isActive();
2626
processPushBundle(isPushPluginActive);
2727

28-
GCMIntentService.cancelNotification(this);
29-
3028
finish();
3129

3230
if (!isPushPluginActive) {

0 commit comments

Comments
 (0)