【问题标题】:Intent data not updated in activity while launched from andorid push notification click从 android 推送通知单击启动时,意图数据未在活动中更新
【发布时间】:2014-05-28 11:06:26
【问题描述】:

我正在为我的 android 应用程序使用 Google 云消息传递。GCM 部分运行成功。现在我想在用户点击状态栏中的通知图标时在 UI 活动中显示消息。

我正在使用以下代码从通知中启动我的 ResultShowActivity

Intent notificationIntent = new Intent(context, ResultShowActivity.class);
   notificationIntent.putExtra("Message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.gcm_cloud)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setSound(alarmSound)
            .setContentText(msg);

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

消息正在正确发送,并在 GCM 通知出现时正确显示,但是当我在 ResultShowActivity 中捕获额外的意图时,它总是显示旧消息。 我正在使用以下代码从 ResultShowActivity 中的意图接收消息。

String GCMMsg = showViewIntent.getStringExtra("Message");  

我尝试使用

删除 ResultShowActivity 中的
intent.removeExtra("Message"); 

intent.replaceExtras(extras);

任何人都可以建议如何从意图中检索更新的消息。感谢您对此提供的任何帮助。

【问题讨论】:

    标签: android android-intent push-notification


    【解决方案1】:

    请使用以下代码

    int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
    
    
    PendingIntent contentIntent = PendingIntent.getActivity(this,iUniqueId,notificationIntent, 0);
    

    而不是下面的代码行

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
    

    希望这会有所帮助

    【讨论】:

    • 很好的提示。非常感谢 ;-)
    【解决方案2】:

    您需要将 Intent.FLAG_ACTIVITY_SINGLE_TOP 添加到意图标志,并将 PendingIntent.FLAG_UPDATE_CURRENT 添加到待处理意图标志。

    Intent intent = new Intent(this,YourActivity.class);
    
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
    PendingIntent pendingIntent = 
    PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    

    重要提示:确保实现onNewIntent(Intent intent)。当您的活动处于前台时,将调用此函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多