首页 > 解决方案 > 活动不是从通知开始的

问题描述

我正在尝试将通知设置为直接指向特定活动。我按照官方文档中列出的步骤进行操作。但是单击通知只会打开应用程序的主启动器。

我试图通过通知启动的活动是 DetailActivity。

这就是我在 manifest.xml 中设置活动层次结构的方式

    <activity
        android:name=".SplashscreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomeActivity"
        android:parentActivityName=".SplashscreenActivity"/>
    <activity
        android:name=".DetailActivity"
        android:parentActivityName=".HomeActivity"/>

在我onMessageReceived的课堂方法中FirebaseMessagingService,我有以下内容:

        Intent resultIntent = new Intent(this, DetailActivity.class);
        // Create the TaskStackBuilder and add the intent, which inflates the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        // Get the PendingIntent containing the entire back stack
        PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationManager.notify(
                NOTIFICATION_ID,
                getNotification("title", "text", intent)
        );

getNotification方法:

private Notification getNotification(String title, String text, PendingIntent intent) {

    return new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(intent)
            .setOngoing(true)
            .build();
}

我不知道这个问题是否是我尝试启动的活动不是启动器活动的直接子项。而且我也不知道如何调试它。希望有人以前遇到过这个奇怪的问题!

标签: androidfirebasegoogle-cloud-firestoreandroid-notifications

解决方案


用这个:

Intent notificationIntent = new Intent(this, DetailActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent. FLAG_ONE_SHOT);

并在通知中使用这个pendingIntent。


推荐阅读