Android 如何从gcm接收多个通知?

Android 如何从gcm接收多个通知?,android,google-cloud-messaging,android-json,Android,Google Cloud Messaging,Android Json,我正在开发一个应用程序,我必须接收来自gcm服务器的通知。我成功地收到了通知。这是我的问题。我已成功读取多个通知,但所有消息通知仅显示一条消息,即仅显示最后一个数据 我的代码 public class GcmIntentService extends IntentService{ Context context; public static int NOTIFICATION_ID = 1; private NotificationManager mNotificati

我正在开发一个应用程序,我必须接收来自gcm服务器的通知。我成功地收到了通知。这是我的问题。我已成功读取多个通知,但所有消息通知仅显示一条消息,即仅显示最后一个数据

我的代码

 public class GcmIntentService extends IntentService{
    Context context;
    public static  int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        Bundle extras = intent.getExtras();
        String msg = intent.getStringExtra("message");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

         if (!extras.isEmpty()) {

             if (GoogleCloudMessaging.
                        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                    sendNotification("Send error: " + extras.toString());
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) {
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                // If it's a regular GCM message, do some work.
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    for (int i=0; i<5; i++) {
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                        }
                    }
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    //sendNotification("Received: " + extras.toString());
                    sendNotification(msg);
                    Log.i(TAG, "Received: " + extras.toString());
                }
            }
         GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);


        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("Event tracker")
        .setContentText("Events received");
    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    String[] events = new String[6];
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Event tracker details:");

    // Moves events into the big view
    for (int i=0; i < events.length; i++) {

        inboxStyle.addLine(events[i]);
    }
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);


        AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

        /* Even if the mode is set to "Sound & Vibration" in the phone, 
         * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
         */
        switch (am.getRingerMode()) 
        {
            case AudioManager.RINGER_MODE_VIBRATE:
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                break;
            case AudioManager.RINGER_MODE_NORMAL:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
                break;
            default:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
         }


        mBuilder.setContentIntent(contentIntent);

      mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());



    }
    public void CancelNotification(Context ctx) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx
                .getSystemService(ns);
        nMgr.cancel(NOTIFICATION_ID);
    }

  }

每个通知都必须有唯一的ID,因此,如果希望它显示为新通知,则必须更改每个通知的通知ID。

更改代码,如下所示:

private void sendNotification(String msg)
{
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent myintent = new Intent(this, ReceiveActivity.class);
    myintent.putExtra("message", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_gcm)
            .setContentTitle("Event tracker").setContentText("Events received");
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String[] events = new String[6];
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Event tracker details:");

    // Moves events into the big view
    for (int i = 0; i < events.length; i++)
    {

        inboxStyle.addLine(events[i]);
    }
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);

    AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

    /* Even if the mode is set to "Sound & Vibration" in the phone, 
     * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
     */
    switch (am.getRingerMode())
    {
    case AudioManager.RINGER_MODE_VIBRATE:
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        break;
    default:
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    }

    mBuilder.setContentIntent(contentIntent);

    // create new notification id
    Date date = new Date();
    NOTIFICATION_ID = (int) date.getTime();

    //now you can notify with newly created notification id
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

public void CancelNotification(Context ctx)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancelAll();
}
private void sendNotification(字符串msg)
{
mNotificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION\u服务);
Intent myintent=新的Intent(这个,ReceiveActivity.class);
myintent.putExtra(“消息”,msg);
PendingEvent contentIntent=PendingEvent.getActivity(this,0,myintent,PendingEvent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder=新的NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle(“事件跟踪器”).setContentText(“收到的事件”);
NotificationCompat.InboxStyle InboxStyle=新NotificationCompat.InboxStyle();
字符串[]事件=新字符串[6];
//设置收件箱样式大视图的标题
inboxStyle.setBigContentTitle(“事件跟踪器详细信息:”);
//将事件移动到大视图中
for(int i=0;i

我希望这可以帮助您在mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build())之后增加通知ID;这statement@Durgaya编写方式类似于mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());通知_ID++;但为唯一通知显示相同的数据上次通知数据\u ID使用System.currentTimeMillis()像mNotificationManager.notify(System.currentTimeMillis(),mBuilder.build());检查此项。谢谢,但它不起作用。我像mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build())那样编写;通知_ID++;
private void sendNotification(String msg)
{
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent myintent = new Intent(this, ReceiveActivity.class);
    myintent.putExtra("message", msg);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_gcm)
            .setContentTitle("Event tracker").setContentText("Events received");
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String[] events = new String[6];
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Event tracker details:");

    // Moves events into the big view
    for (int i = 0; i < events.length; i++)
    {

        inboxStyle.addLine(events[i]);
    }
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);

    AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

    /* Even if the mode is set to "Sound & Vibration" in the phone, 
     * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
     */
    switch (am.getRingerMode())
    {
    case AudioManager.RINGER_MODE_VIBRATE:
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        break;
    default:
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    }

    mBuilder.setContentIntent(contentIntent);

    // create new notification id
    Date date = new Date();
    NOTIFICATION_ID = (int) date.getTime();

    //now you can notify with newly created notification id
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

public void CancelNotification(Context ctx)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancelAll();
}