【问题标题】:Why PendingIntent opens the launcher activity of the app rather than specific activity?为什么 PendingIntent 打开应用的启动器活动而不是特定活动?
【发布时间】:2020-04-13 13:52:47
【问题描述】:

我正在使用 firebase 发送通知。当用户单击通知时,它将打开 ResultActivity。当应用程序处于前台时,它工作正常。但是当应用程序在后台时,它会打开 HomeActivity(这是应用程序的启动器活动)而不是 ResultActivity。我不明白这是什么问题?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);

        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

【问题讨论】:

标签: android android-pendingintent firebase-notifications


【解决方案1】:

这是测试 click_action 映射的好方法。它需要一个类似于 FCM 文档中指定的意图过滤器:

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

另外,请记住,这仅在应用程序在后台时才有效。如果它在前台,您将需要实现 FirebaseMessagingService 的扩展。在 onMessageReceived 方法中,您需要手动导航到 click_action 目标

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    //This will give you the topic string from curl request (/topics/news)
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    //This will give you the Text property in the curl request(Sample     Message): 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    //This is where you get your click_action 
    Log.d(TAG, "Notification Click Action: " + remoteMessage.getNotification().getClickAction());
    //put code here to navigate based on click_action
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2020-05-05
    • 2011-02-23
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多