从通知启动 Activity - Android Studio

Posted

技术标签:

【中文标题】从通知启动 Activity - Android Studio【英文标题】:Starting Activity from a notification - Android Studio 【发布时间】:2020-04-06 22:33:42 【问题描述】:

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

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

实现了这一点

android:launchMode="singleTop"

在我的清单中。

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

这是我的通知代码:

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);
    

【问题讨论】:

【参考方案1】:

试试下面的代码

在通知中添加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);

希望对你有帮助!

谢谢。

【讨论】:

感谢您的回答。我尝试了您建议的标志,但到目前为止没有帮助。当我第一次从此通知打开活动时,它工作正常。但是,当我点击另一个通知并尝试再次打开此活动时,但使用不同的数据,我只是回到不适合的旧活动。你明白我的意思吗? 查看我的通知代码,我将其添加到问题中。非常感谢【参考方案2】:

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

【讨论】:

【参考方案3】:

试试下面

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);

【讨论】:

以上是关于从通知启动 Activity - Android Studio的主要内容,如果未能解决你的问题,请参考以下文章

android从通知栏跳转到相应的activity,回退到前一个activity的问题?

Android CGM 收到推送通知后启动 Activity

由于 Activity 意图,Android 通知点击导致应用程序崩溃

从通知启动时防止重新创建活动活动

无论应用程序状态如何,都可以从通知中正确启动 Activity

Android实现点击通知栏后,先启动应用再打开目标Activity ,极光推送等推送的也可以参考一下(转)