gpt4 book ai didi

java - Android通知不断启动应用程序

转载 作者:行者123 更新时间:2023-11-30 02:09:10 26 4
gpt4 key购买 nike

我正在创建一个通知,当收到通知时应用程序启动。我的问题是如何阻止这种情况发生?

final private static void generateNotification(Context context, String message){
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}

在GCMIntentService中调用通知:

@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String response = intent.getExtras().getString("response");

switch(response){
case "logout":
String message = "You logged into another device!";
//displayMessage(context, message);
// notifies user
generateNotification(context, message);
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
break;
default:

break;
}



}

最佳答案

只是不要执行我在下面注释掉的这些行。该代码在接收到 “注销” 消息时启动一个 Activity 。

generateNotification(context, message);
//Intent i = new Intent(context, MainActivity.class);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(i);

关于java - Android通知不断启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352949/

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