gpt4 book ai didi

android - 单击通知没有开始预期的 Activity ?

转载 作者:IT老高 更新时间:2023-10-28 22:16:08 27 4
gpt4 key购买 nike

我在我的应用程序中使用 GCM,并在收到 GCM 消息时使用 NotificationManager 创建通知。到目前为止,一切正常,GCM 消息在通知区域中正确显示,但是当我单击通知时它应该启动我的应用程序的一个 Activity ,它将显示未发生的消息详细信息。每次我点击通知时,它都不会启动任何 Activity ,它保持原样。我创建通知的代码是:

private void sendNotification(String msg) {
SharedPreferences prefs = getSharedPreferences(
DataAccessServer.PREFS_NAME, MODE_PRIVATE);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, WarningDetails.class);
Bundle bundle = new Bundle();
bundle.putString("warning", msg);
bundle.putInt("warningId", NOTIFICATION_ID);
intent.putExtras(bundle);
// The stack builder object will contain an artificial back stack for
// the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WarningDetails.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);

PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.weather_alert_notification)
.setContentTitle("Weather Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
String selectedSound = prefs.getString("selectedSound", "");
if (!selectedSound.equals("")) {
Uri alarmSound = Uri.parse(selectedSound);
mBuilder.setSound(alarmSound);

} else {
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
}

if (prefs.getBoolean("isVibrateOn", false)) {
long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 };
mBuilder.setVibrate(pattern);
}

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

我更新了我的代码以支持 Preserving Navigation when Starting an Activity 就像它发生在使用 Android 开发人员网站的 Gmail 应用程序中一样,此后它停止工作。有人请指导我我缺少什么或在做什么这段代码错了。

最佳答案

我的问题解决了,我只需要添加 PendingIntent.FLAG_ONE_SHOT 标志,所以我替换了:

PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);

关于android - 单击通知没有开始预期的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20743998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com