GCM消息得到覆盖? [英] GCM message is getting overridden?

查看:118
本文介绍了GCM消息得到覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我使用GCM推送通知传递一些通知给用户。我的问题是,当我发送一个信息,那么这个工作正常,如果发送多则最后一条消息显示给所有的通知。 帮我在这,是我犯的错误... ??

 私有静态无效generateNotification(上下文的背景下,字符串消息){
  INT图标= R.drawable.ic_launcher;
  时长= System.currentTimeMillis的();
  NotificationManager notificationManager =(NotificationManager)
          context.getSystemService(Context.NOTIFICATION_SERVICE);

  意图notificationIntent =新的意图(背景下,GCMMessageView.class);
  的String [] messageArray = message.split(\\#);
  //参数::的agentId,名称,消息
  DatabaseHelper DB =新DatabaseHelper(上下文);
  INT notificationId = db.insertnotifications(messageArray [1],messageArray [0],messageArray [messageArray.length-1]);

  notificationIntent.putExtra(消息,messageArray [messageArray.length-1]);
  //设置的意图,因此它不会开始一个新的活动
  notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  PendingIntent意图= PendingIntent.getActivity(上下文,0,notificationIntent,0);

  通知通知=新NotificationCompat.Builder(上下文)
        .setContentText(messageArray [messageArray.length-1])
        .setContentTitle(context.getString(R.string.app_name))
        .setSmallIcon(图标)
        .setWhen(时)
        .setContentIntent(意向)
        。建立();

    notification.flags | = Notification.FLAG_AUTO_CANCEL;

    //播放默认通知声音
    notification.defaults | = Notification.DEFAULT_SOUND;

    //如果振动被启用时振动
    notification.defaults | = Notification.DEFAULT_VIBRATE;
    notificationManager.notify(notificationId,通知);
}
 

解决方案

请通知ID唯一的每个message.Due这个它是覆盖

Hi All am using GCM push notification to pass some notification to the user. my problem is when am sending single message then this works fine if send more than one then the last message is shown to all the notification. Help me on this , were i made mistake ... ??

  private static void generateNotification(Context context, String message) {
  int icon = R.drawable.ic_launcher;
  long when = System.currentTimeMillis();
  NotificationManager notificationManager = (NotificationManager)
          context.getSystemService(Context.NOTIFICATION_SERVICE);

  Intent notificationIntent = new Intent(context, GCMMessageView.class);
  String[] messageArray = message.split("\\#");
  // param :: agentId, Name, Msg
  DatabaseHelper db = new DatabaseHelper(context);
  int notificationId = db.insertnotifications(messageArray[1], messageArray[0], messageArray[messageArray.length-1]);

  notificationIntent.putExtra("message", messageArray[messageArray.length-1]);
  // set intent so it does not start a new activity
  notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

  Notification notification = new NotificationCompat.Builder(context)
        .setContentText(messageArray[messageArray.length-1])
        .setContentTitle(context.getString(R.string.app_name))
        .setSmallIcon(icon)
        .setWhen(when)
        .setContentIntent(intent)
        .build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(notificationId, notification);
}

解决方案

Make notification id unique for each message.Due to this it's being override

这篇关于GCM消息得到覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆