捆绑数据未在服务中的通知传递的活动中更新,始终填充第一个捆绑包

时间:2013-10-10 15:27:29

标签: android android-notifications

我正在使用GCMIntentService在我的应用程序中处理推送通知。我正在创建状态栏通知并使用待处理的Intent导航到活动。

我创建通知的代码是:

private static void generateNotification(Context context, String message, String  title1, String desc, String soln, String date, String time) {
 int icon = R.drawable.notification_icon;
    long when = System.currentTimeMillis();
        Log.i("","@@##@@ notificatin"+title1+desc+soln+message);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context,AppLog.class);
    notificationIntent.putExtra(Description.REQUEST_FROM, "notification");
    notificationIntent.putExtra(Description.INFO_TITLE, title1);
    notificationIntent.putExtra(Description.INFO_DESC, desc);
    notificationIntent.putExtra(Description.INFO_SOLUTION, soln);
    notificationIntent.putExtra(Description.INFO_DATE, date);
    notificationIntent.putExtra(Description.INFO_TIME, time);
    PendingIntent pIntent = PendingIntent.getActivity(context,  0,notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, pIntent);
    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notification.sound=uri;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

在我的AppLog.class中,我正在处理它:

 if(bundle!=null && bundle.containsKey(Description.REQUEST_FROM)){

        Log.i("","@@##@@ applog"+bundle);

    }

当第一个通知发送到设备时,数据将在我的AppLog Activity类中正确填充。但是对于所有通知,它总是向我显示旧捆绑。

我尝试了一切,但问题仍然存在。是否存在从服务创建的待处理意图或通知的任何问题?

2 个答案:

答案 0 :(得分:1)

这有效

PendingIntent pIntent = PendingIntent.getActivity(context,  0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

答案 1 :(得分:1)

不要总是传递0值作为第二个参数,而是尝试并传递一个随机数。

Random r = new Random();
        int no = r.nextInt(999999);

     PendingIntent pIntent = PendingIntent.getActivity(context,  no,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);