获取多条GCM消息

时间:2016-01-31 07:28:08

标签: java android google-cloud-messaging message

这个类不会处理多个GCM消息并在通知栏上显示它们,目前只处理一个,如何才能使它处理多个消息?

public class GCMIntentService extends GcmListenerService {
    private static final String TAG = "GCMIntentService";
    int notifyid = 0;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "from:" + from);
        Log.d(TAG, "message:" + message);
        sendNotification(message);
    }

    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notifyid, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("New Messsage")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentIntent(pendingIntent);
            int numMessages = 0;
            notificationBuilder.setContentText(message).setNumber(++numMessages);
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notifyid, notificationBuilder.build());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

来自the documentation

  

public void notify (int id, Notification notification)

     

发布要在状态栏中显示的通知。 如果您的应用程序已经发布了具有相同ID的通知但尚未取消,则该通知将被更新的信息替换。

notifyid似乎从未在您发布的代码中发生变化,这就是您只看到一个通知的原因。