我需要帮助接收BroadcastReceiver中的Android推送通知

时间:2015-03-25 16:01:00

标签: android android-notifications android-broadcast android-notification-bar

我正在使用Parse.com进行推送通知。为了处理自定义通知,我创建了自己的广播接收器。

从Parse服务器,我得到了额外的JSON数据,如下所示:

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

因此,在 onReceive()方法中,我可以通过JSON上方的“来自”键获取邮件发件人。

如果只有一个用户发送消息,一切正常,但如果多个用户发送消息通知,则只显示上一个用户的通知。

另外,正如您在下面的代码中看到的,我跟踪numMessages,因此我可以显示收到的消息的计数。

当我点击并打开该通知时,numMessages计数器是否应重置为0?

问题:如何跟踪多个用户是否发送了消息,以便为每个用户显示单独的通知?

以下是 MyReceiever 类:

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 个答案:

没有答案