Ожидание намерения открывает неправильную активность - PullRequest
0 голосов
/ 18 мая 2018

Я использую FirebaseMessagingService для получения уведомлений и открытия приложения после нажатия на уведомление. Но каждый раз, когда я нажимаю на уведомление, приложение открывает MainActivity вместо предполагаемой ResultActivity. Я также следовал документам из документов PendingIntent и до сих пор делаю то же самое

    private void createNotification( String messageBody) {
        Intent intent = new Intent( this , ResultActivity.class );

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
//        PendingIntent resultPending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                .setContentTitle("VERA")
                .setContentText(messageBody)
                .setAutoCancel( true )
                .setSound(notificationSoundURI)
                .setContentIntent(resultIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        notificationManager.notify(0, mNotificationBuilder.build());
    }

Вот мой Манифест.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
    <activity android:name=".ResultActivity"
        android:launchMode="singleTask"
        android:excludeFromRecents="true"
        android:taskAffinity=""></activity>

РЕДАКТИРОВАТЬ: Я пытался передать некоторые дополнительные строки, но основное действие даже не получает ничего. Возможно ли, что уведомление запускает только метод по умолчанию для запуска приложения?

Ответы [ 2 ]

0 голосов
/ 18 мая 2018

Очевидно, что нет способа вызвать onMessageReceived, используя только консоль FCM. Он будет срабатывать только в том случае, если я буду использовать другие способы отправки сообщений с данными.

0 голосов
/ 18 мая 2018

Создание намерения, которое запускает действие.

Настройте запуск действия в новой пустой задаче, вызвав setFlags() с флагами FLAG_ACTIVITY_NEW_TASK и FLAG_ACTIVITY_CLEAR_TASK.

Создайте PendingIntent, вызвав getActivity().

Как,

Intent notifyIntent = new Intent(this, ResultActivity.class);
    // Set the Activity to start in a new, empty task
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // Create the PendingIntent
    PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, 
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Затем вы можете передать PendingIntent как обычно:

NotificationCompat.Builder mNotificationBuilder= new NotificationCompat.Builder(this, "CHANNEL_ID");
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mNotificationBuilder.build());
...