Открытое диалоговое окно при нажатии на уведомление (FCM) - PullRequest
0 голосов
/ 01 октября 2018

Я хочу отобразить заголовок и текст сообщения в диалоговом окне при нажатии на уведомление.Вот мой код с onMessageReceived и SendNotification, я использую fcm из консоли для отправки сообщения -

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    sendNotification(remoteMessage.getNotification().getBody());

}


private void sendNotification (String messageBody){

    Intent intent = new Intent(this ,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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


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

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.ic_stat_name);
    notificationBuilder.setContentTitle("Guru Nanak Dev Polytechnic College");
    notificationBuilder.setContentText(messageBody);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(defaultsoundUri);
    notificationBuilder.setContentIntent(pendingIntent);


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

}
}

Ответы [ 2 ]

0 голосов
/ 01 октября 2018

В вашем PendingIntent для MainActivity отправьте messageBody как EXTRA, т. Е.

Intent intent = new Intent(this ,MainActivity.class);
intent.putExtra("data", messageBody);

Теперь, когда пользователь нажимает на уведомление, он переводит их в MainActivity.В onCreate() MainActivity проверьте, открывается ли это действие из уведомления или где-то еще (с помощью getIntent), например:

String notificationData = getIntent().getStringExtra("data");

//if this activity is opened from notification, it will have extra data
if(notificationData !=null){

 //now open dialog
 showDialog(notificationData);
}
0 голосов
/ 01 октября 2018

Для отображения диалогового окна вам понадобится ссылка на действие:

Intent intent = new Intent(this ,MainActivity.class);
intent.putExtra("fromNotification",true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

В MainActivity:

Intent intent= getIntent;
        if(intent.getBooleanExtra("fromNotification",true))
        {
            // show your dialog here
        }
...