从通知开始 Activity - Android Studio

Starting Activity from a notification - Android Studio

我正在尝试通过通知启动 activity。在开始 activity 后,我通过 intent.putextra 添加数据到意图中,以便 activity 显示适合该情况的正确内容。

正在启动的 activity 应该在堆栈中只打开一次。我确实通过

实现了这一点

android:launchMode="singleTop"

在我的清单中。

但是 - 现在我来回答我的问题 - 如果这个 activity 已经是 运行,我希望它关闭并用我正在创建的实例替换它,其中包含特定的附加数据 (放额外的)。我怎样才能做到这一点?

这是我的通知代码:

public void newMessageNotification(String title, String message, String otherusernumber, String requestStatus, String sendername) {


        notificationManager = NotificationManagerCompat.from(this);

        Intent chatIntent = new Intent(this,ChatActivity.class);

        //these three strings define the behaviour of the chat activtity

        chatIntent.putExtra("otherUsername", sendername);
        chatIntent.putExtra("otherUsernumber", otherusernumber);
        chatIntent.putExtra("requestStatus", requestStatus);


        chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), chatIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);;


        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_new_message)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .build();

        notificationManager.notify(1, notification);
    }

尝试添加标记:Intent.FLAG_ACTIVITY_NEW_TASK 到您的 Intent

试试下面

Intent intent = new Intent(this, ActivityDestination.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

//pendingIntent  = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ACTIVITY_NEW_TASK);

试试下面的代码

在通知中添加PendingIntent

Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

完整创建通知代码

Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager notificationManager =
        (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
        Uri defaultSoundUri = RingtoneManager . getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);

        Notification notification;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // The id of the channel.
            String Ch_id = "yourappname_01";
            // The user-visible name of the channel.
            CharSequence name = "Notification";
            // The user-visible description of the channel.
            //String description = getString(R.string.channel_description);
            int importance = NotificationManager . IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(Ch_id, name, importance);
            mChannel.setSound(defaultSoundUri, new AudioAttributes . Builder ().build());
            notificationManager.createNotificationChannel(mChannel);
            // Create a notification and set the notification channel.
            notification = new Notification . Builder (this, Ch_id)
            .setSmallIcon(R.drawable.notify)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(remoteMessage.getData().get("title"))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent) //Add PendingIntent
                .setChannelId(Ch_id)
                .build();
        } else {
            // Create a notification
            notification = new Notification . Builder (this)
                .setSmallIcon(R.drawable.notify)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(remoteMessage.getData().get("title"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent) //Add PendingIntent
                .build();
        }
        //Generate Diff Notification
        int m =(int)((new Date ().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notification);

更新

Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
                PendingIntent.FLAG_ONE_SHOT);

希望对您有所帮助!

谢谢。