Skip to content

Commit 4f0d58d

Browse files
authored
Merge pull request avivais#75 from quilligana/optional-multiple-notifications-android
FIX: (Android) Opening incorrect notification from notification tray
2 parents 93ecf51 + c026d36 commit 4f0d58d

1 file changed

Lines changed: 43 additions & 32 deletions

File tree

src/android/ParsePushPluginReceiver.java

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,38 @@ public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver
3838

3939
@Override
4040
protected void onPushReceive(Context context, Intent intent) {
41-
if(ParsePushPlugin.isInForeground()){
42-
//
43-
// relay the push notification data to the javascript
44-
ParsePushPlugin.jsCallback( getPushData(intent) );
41+
if(ParsePushPlugin.isInForeground()){
42+
//
43+
// relay the push notification data to the javascript
44+
ParsePushPlugin.jsCallback( getPushData(intent) );
45+
} else {
46+
//
47+
// only create entry for notification tray if plugin/application is
48+
// not running in foreground.
49+
//
50+
// So first we check if the user has set the configuration to have multiple
51+
// notifications show in the tray (i.e. set <preference name="ParseMultiNotifications" value="true" />)
52+
ParsePushConfigReader config = new ParsePushConfigReader(context, null, new String[] {"ParseMultiNotifications"});
53+
String parseMulti = config.get("ParseMultiNotifications");
54+
if(parseMulti != null && !parseMulti.isEmpty() && parseMulti.equals("true")){
55+
// If the user wants multiple notifications in the tray, then we let ParsePushBroadcastReceiver
56+
// handle it from here
57+
super.onPushReceive(context, intent);
4558
} else {
46-
//
47-
// only create entry for notification tray if plugin/application is
48-
// not running in foreground.
49-
//
50-
// So first we check if the user has set the configuration to have multiple
51-
// notifications show in the tray (i.e. set <preference name="ParseMultiNotifications" value="true" />)
52-
ParsePushConfigReader config = new ParsePushConfigReader(context, null, new String[] {"ParseMultiNotifications"});
53-
String parseMulti = config.get("ParseMultiNotifications");
54-
if(parseMulti != null && !parseMulti.isEmpty() && parseMulti.equals("true")){
55-
// If the user wants multiple notifications in the tray, then we let ParsePushBroadcastReceiver
56-
// handle it from here
57-
super.onPushReceive(context, intent);
58-
} else {
59-
// use tag + notification id=0 to limit the number of notifications in the tray
60-
// (older messages with the same tag and notification id will be replaced)
61-
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
62-
notifManager.notify(getNotificationTag(context, intent), 0, getNotification(context, intent));
63-
}
64-
//
65-
// A user with Android 5.0.1 reports that notif is not created in tray when
66-
// app is off (not background), trying method described here
67-
// https://github.com/phonegap/phonegap-plugin-push/issues/211 by @vikasing
68-
// to see if it works
69-
//
70-
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
71-
setResultCode(Activity.RESULT_OK);
59+
// use tag + notification id=0 to limit the number of notifications in the tray
60+
// (older messages with the same tag and notification id will be replaced)
61+
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
62+
notifManager.notify(getNotificationTag(context, intent), 0, getNotification(context, intent));
63+
64+
//
65+
// A user with Android 5.0.1 reports that notif is not created in tray when
66+
// app is off (not background), trying method described here
67+
// https://github.com/phonegap/phonegap-plugin-push/issues/211 by @vikasing
68+
// to see if it works
69+
//
70+
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
71+
setResultCode(Activity.RESULT_OK);
72+
}
7273
}
7374
}
7475

@@ -118,9 +119,19 @@ protected Notification getNotification(Context context, Intent intent){
118119
cIntent.putExtras(intent).setPackage(context.getPackageName());
119120
dIntent.putExtras(intent).setPackage(context.getPackageName());
120121

121-
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
122-
PendingIntent deleteIntent = PendingIntent.getBroadcast(context, 0, dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
122+
int contentIntentRequestCode = 0;
123+
int deleteIntentRequestCode = 0;
124+
125+
ParsePushConfigReader config = new ParsePushConfigReader(context, null, new String[] {"ParseMultiNotifications"});
126+
String parseMulti = config.get("ParseMultiNotifications");
127+
if(parseMulti != null && !parseMulti.isEmpty() && parseMulti.equals("true")){
128+
Random random = new Random();
129+
contentIntentRequestCode = random.nextInt();
130+
deleteIntentRequestCode = random.nextInt();
131+
}
123132

133+
PendingIntent contentIntent = PendingIntent.getBroadcast(context, contentIntentRequestCode, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
134+
PendingIntent deleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
124135

125136
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
126137

0 commit comments

Comments
 (0)