Неправильная активность при нажатии на уведомление (PendingIntent) - PullRequest
0 голосов
/ 28 мая 2018

Я реализовал службу уведомлений в своем приложении для Android (FirebaseMessagingService).Когда я нажимаю на уведомление, и приложение закрывается, открывается неправильное действие.Если уведомление приходит, когда приложение открыто (передний план, фон), то открытое действие корректно.

В следующем коде всплывающее действие (первое действие проекта) открывается вместо IntroNoti.учебный класс.Это код моего класса FireBaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    //Log.d(TAG, "From: " + remoteMessage.getFrom());
    //Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    Globals.messageIn = true;
    sendNotification(remoteMessage);
}

private void sendNotification(RemoteMessage remoteMessage) {

    //Intent intent = new Intent(this, Login.class);
    Intent intent = new Intent(this, IntroNoti.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_launcher: R.mipmap.ic_launcher;
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentText("CIAO CIAO")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

1 Ответ

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

Благодаря совету @Eugen Pechanec я добавил эту инструкцию в свой MainActivity.Свойство messageid оценивается только тогда, когда приложение открывается уведомлением.Спасибо вам всем!

if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().size() > 0) {
        Log.d(TAG, "Received extras in onCreate()");
        Bundle extras = getIntent().getExtras();
        if (!extras.getString("messageid","").isEmpty()) {
            Globals.fromNotification = true;
        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...