【问题标题】:Starting Activity from a notification - Android Studio从通知启动 Activity - 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);
    }

【问题讨论】:

    标签: android android-intent android-activity notifications


    【解决方案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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多