【问题标题】:Trigger Method in Activity when Notification is tapped点击通知时Activity中的触发方法
【发布时间】:2019-11-16 05:09:13
【问题描述】:

我的应用反复从服务器加载数据。这些数据中有消息。
每当加载新消息时,都会在接口的回调方法 onMessagesReceived(int numOfMessages) 上调用 MainActivity。
该应用程序只有一个 Activity,每个屏幕都实现为 Fragment。片段的切换由专用的 Navigator 类管理。

我的问题是用户点击通知的处理。当用户点击通知时,应显示消息列表。

public class MainActivity extends AppCompatActivity implements MessageListener {
    private static final int MESSAGE_NOTIFICATION_ID = 101010;
    private static final String EXTRA_SHOW_MESSAGES = "SHOW_MESSAGES";

    private Navigator mNavigator;

    @Override
    onMessagesReceived(int numOfMessages) {
        Intent intent = new Intent(this, MainActivity.class);
        testIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        testIntent.putExtra(EXTRA_SHOW_MESSAGES, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "testChannel")
                .setSmallIcon(R.drawable.ic_message_notification)
                .setContentTitle("New messages!")
                .setContentText("You got " + numOfMessages + " new messages")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(MESSAGE_NOTIFICATION_ID, builder.build());
    }

    @Override
    protected void onResume() {
        super.onResume();

        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.containsKey(EXTRA_SHOW_MESSAGES)) {
            if (extras.getBoolean(EXTRA_SHOW_MESSAGES)) {
                mNavigator.openMessageList();
            }
        }
    }
}

此时,MainActivity 显示,当应用程序处于后台时,但在 onResume 中,Bundle 返回为 null
当应用程序在前台时,什么都不会发生。

我想实现点击Notification:
- 当用户在应用程序内时,应该打开 MessageList Fragment
- 当用户不在应用内时,应在打开MessageList Fragment之前启动

有人可以给我一个提示,如何从这里开始?也许在这里使用 Intents 不是正确的解决方案?

【问题讨论】:

    标签: android android-intent notifications android-pendingintent


    【解决方案1】:

    您可以使用意图,只需在意图中添加一些布尔附加值,然后检查主要活动中的值,如果该附加值为真,然后调用您的方法。

    【讨论】:

    • 你是对的,但最后没关系,如果我添加一个布尔值或一个字符串作为额外的。只要在 Activity 已经显示的情况下我无法获得额外的东西,我就无法对任何额外的东西做出反应。
    【解决方案2】:

    在对 Intents 和 Notifications 进行了更多研究之后,我终于想出了一个解决方案。

    public class MainActivity extends AppCompatActivity implements MessageListener {
        private static final int MESSAGE_NOTIFICATION_ID = 101010;
        private static final String EXTRA_SHOW_MESSAGES = "SHOW_MESSAGES";
    
        private Navigator mNavigator;
    
        @Override
        onMessagesReceived(int numOfMessages) {
            Intent intent = new Intent(this, MainActivity.class);
            testIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            testIntent.putExtra(EXTRA_SHOW_MESSAGES, true);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "testChannel")
                    .setSmallIcon(R.drawable.ic_message_notification)
                    .setContentTitle("New messages!")
                    .setContentText("You got " + numOfMessages + " new messages")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent);
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(MESSAGE_NOTIFICATION_ID, builder.build());
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            this.setIntent(intetnt)
    
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(EXTRA_SHOW_MESSAGES)) {
                if (extras.getBoolean(EXTRA_SHOW_MESSAGES)) {
                    mNavigator.openMessageList();
                }
            }
        }
    }
    

    我将读取新 Intent 的代码移至 onNewIntent。当一个 Activity 获得一个新的 Intent 并且在 onResume 之前调用这个方法。无论 Activity 是否在前台,这都会触发。我还用 setIntent 将这个新 Intent 设置为活动 Intent,否则最初启动我的 Activity 的 Intent 将由 getIntent() 调用。

    【讨论】:

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