【问题标题】:Send putExtra via notification intent通过通知意图发送 putExtra
【发布时间】:2015-05-26 14:06:24
【问题描述】:

我想根据代码打开一个特定的选项卡。如果我通过普通按钮执行此操作,它会起作用。

它的工作方式:

public void toSomewhere (View view) {
    Intent intent = new Intent(this, SomewhereActivity.class);
    intent.putExtra("FirstTab", 2);
    startActivity(intent);
}

Intent i = getIntent();
int tabToOpen = i.getIntExtra("FirstTab", -1);
if (tabToOpen!=-1) {
    // Open the right tab
}
else {
    // Other tab
}

我希望它通过通知工作的方式(此时我有这个发送通知的代码,但不给 .putExtra 通过):

public static void NotificationIntent(String title, String message) {
    Intent notificationIntent = new Intent(currentContext, SomewhereActivity.class);
    **notificationIntent.putExtra("FirstTab", 2);**
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(currentContext, 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder = new    NotificationCompat.Builder(currentContext)
            .setContentTitle(title)
            .setContentIntent(intent)
            .setContentText(message)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
    NotificationManager mNotificationManager = (NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}

有谁知道如何解决这个问题,所以它也适用于通知帖子?

【问题讨论】:

标签: android


【解决方案1】:

使用FLAG_UPDATE_CURRENT

来自文档:

标志表示如果描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换其额外数据。用于 getActivity(Context, int, Intent, int)、getBroadcast(Context, int, Intent, int) 和 getService(Context, int, Intent, int)。

如果您正在创建仅附加内容发生变化的意图,则可以使用此选项,并且不在乎任何收到您之前的 PendingIntent 的实体都能够使用您的新附加内容启动它,即使它们没有明确提供给它.

代码:

Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("NotificationMessage", notificationMessage);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

onNewIntent方法的Activity中:

@Override
public void onNewIntent(Intent intent){
    Bundle extras = intent.getExtras();
    String notificationMessage = extras.getString("NotificationMessage", "UNDEFINED");
}

也可以在onCreate 中转接电话:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());
}

【讨论】:

  • 谢谢。 String notificationMessage = extras.getString("NotificationMessage", "UNDEFINED"); 是不必要的,但其余代码运行良好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 2017-07-03
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多