点击通知时 Activity 中的触发方法

Trigger Method in Activity when Notification is tapped

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

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

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 returns 作为 .
当应用程序在前台时,什么也不会发生。

我想实现点击通知:
- 当用户在应用内时,应打开 MessageList 片段
- 当用户不在应用程序内时,应在打开 MessageList 片段之前启动它

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

您可以使用 intents,只需在 intent 中添加一些布尔值,然后检查 main activity 中的值,如果该额外值为真,则调用您的方法。

在深入研究 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 是否在前台,都会触发。我还将这个新 Intent 设置为 setIntent 的 Activities Intent,否则最初启动我的 Activity 的 Intent 由 getIntent().