Android : I need help on receiving Android push notification in BroadcastReciever

on Wednesday, March 25, 2015


I am using Parse.com for push notifications. To handle custom notifications I have created my own broadcast receiver.


From Parse server I get Intent with extra JSON data, that looks like this:



{
"data":{
"badge":"Increment",
"alert":"Zoran : bgg",
"messageId":"aqwbduZDLf",
"from":"lKVl2ahxkS",
"name":"Zoran "
}
}


So on onReceive() method I can get who is the message sender with "from" key from above JSON.


And everything works well if just one user sends message, but if multiple users send message notification shows just the notification from the last user.


Also, as you can see in the below code, I track numMessages, so I can display count of the received messages.


When I click and open that notification, should that numMessages counter reset to 0?


QUESTION: How I can track if multiple users send message, so I can show separate notification for each user?


Here is MyReceiever class:



public class MyReciever extends ParsePushBroadcastReceiver {

private static final String TAG = "MyCustomReceiver";
private HashMap<String, String> dataMap;
public static int numMessages = 0;
private static final int SINGLE_NOTIFICATION = 1;
String from;
NotificationManager mNotifM;

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);

if (intent == null) {
Log.d(TAG, "Receiver intent null");
} else {
String action = intent.getAction();
Log.d(TAG, "got action " + action);
JSONObject json = null;
try {
json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

} catch (JSONException e) {
e.printStackTrace();
}

try {
JSONObject jsonObject = json.getJSONObject("data");
Iterator itr = jsonObject.keys();
dataMap = new HashMap<String, String>();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "key: " + key);
try {
String value = jsonObject.getString(key);
Log.d(TAG, "value: " + value);
dataMap.put(key, value);

} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}



if (dataMap.containsKey("from")) {
if(!dataMap.get("from").equals(ParseUser.getCurrentUser().getObjectId()) ) {

generateNotification(context, "NOTIFICATION TITLE", dataMap.get("alert"), from);

}
}


private void generateNotification(Context context, String title, String msg, String from) {
Intent intent = new Intent(context, MessagesActivity.class);
intent.putExtra("user_id", from);
from = dataMap.get("from");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle(title);
mBuilder.setContentText(msg);
mBuilder.setNumber(numMessages);
Log.d("TAG", "numMessages: "+String.valueOf(numMessages));
mBuilder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setGroupSummary(true);
mNotifM.notify(SINGLE_NOTIFICATION, mBuilder.build());
}


}


0 comments:

Post a Comment