gpt4 book ai didi

android - 每次打开应用程序时显示 GCM 消息后的 AlertDialog

转载 作者:行者123 更新时间:2023-11-30 03:58:01 26 4
gpt4 key购买 nike

我正在接收来自 GCM 的消息。

这是我在 GCMBaseIntentService 中处理接收消息的方式:

@Override
protected void onMessage(Context context, Intent intent) {
String msg = intent.getExtras().getString("message");
generateNotification(context, msg);
}

private static void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.putExtra("message", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, notification);
}

MyClass 中,我在 onResume 中有这个:

String msg = this.getIntent().getStringExtra("message");
if(msg != null){
new AlertDialog.Builder(this)
.setTitle("New Notification")
.setMessage(msg)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}

GCM 消息出现在状态栏中。单击通知后,MyClass 打开并出现上面的 AlertDialog

我通过转到新 Activity 、单击后退或单击主页离开 MyClass。当我返回该 Activity 时,每次返回时都会出现“AlertDialog”。

我该如何防止这种情况发生?我认为 AlertDialog 只会在点击状态栏中的通知后立即出现一次。

更新:

所以我认为正在发生的事情是,当我从 GCM 消息创建通知 (generateNotification) 时,它会使用这个新 Intent 打开 Activity 。现在每次打开此 Activity 时,都会重复使用相同的 Intent ,因此会再次读取额外内容,然后显示警报。不过,我仍然不知道如何阻止它。

我想我将尝试存储一个带有 Intent 时间戳的 SharedPreference 作为额外的。然后,如果 msg == null 并且时间戳是新的,我将只显示警报。

我喜欢 devunwired 的回答,但以防万一有人好奇,将时间戳存储在共享首选项中也很有效。

这就是我实现它的方式:(在 MyClass 我在 onResume 中有这个)

String msg = this.getIntent().getStringExtra("message");
if(msg != null){
long newTime = intent.getLongExtra("intentTime", 0);
long oldTime = preferences.getLong("intentTime", 0);

if(newTime > oldTime){
new AlertDialog.Builder(this)
.setTitle("New Notification")
.setMessage(msg)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
}

最佳答案

Intent 仍然附加到 Activity 是正确的,因此每次进入 onResume() 都会执行该代码。它不一定会被重复使用,只是您收到的 Intent 始终是启动实例的那个,如果您导航到新屏幕并返回,或者点击HOME 键,因为当前实例仍然存在(它只是暂时停止)。

奇怪的是你说它也发生在 BACK 上。在 BACK 按钮的情况下,它应该销毁 Activity 实例,这样新的启动将有一个不同的 Intent 与之关联。除非您下次启动它时是从同一个通知或最近的菜单(这将与以前的 Intent 一起启动)。

您可以尝试将此逻辑移动到 onCreate() 中,它只会在新实例开始运行时被调用。另一种选择是在使用新的空白 Intent 处理传入通知后调用 Activity 上的 setIntent(),这样后续条目就不会启动该代码。

关于android - 每次打开应用程序时显示 GCM 消息后的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13036981/

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