2

Я переопределил метод получения уведомления при открытом приложении:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService implements Constants{

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if(remoteMessage.getData().get("URL")==null){
            sendNotification(remoteMessage.getNotification().getBody(),URL_HODITE_COM);
        }
        else{
            sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("URL"));
        }

    }

    private void sendNotification(String body,String url) {
        Intent intent=new Intent(this,WebActivity.class);
        intent.putExtra(KEY_INTENT,url);

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

        Notification.Builder builder = new Notification.Builder(this);
// оставим только самое необходимое
        builder.setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis()) //Время уведомления
                .setSmallIcon(R.mipmap.ico)
                .setContentTitle("Hodite")
                .setContentText(body); // Текст уведомления

        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND |
                Notification.DEFAULT_VIBRATE;
// ставим флаг, чтобы уведомление пропало после нажатия
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        nm.notify((body+url).hashCode(),notification);
    }
}

Но когда приходит уведомление при выключенном приложении, он открывает просто приложение.

Как мне сделать так, чтобы у меня при выключенном приложении срабатывало всё так же, как при включенном: одним словом выполнялся тот же код, который выше?

1 ответ 1

1

Используйте getIntent.getExtras или getStringExtra для получения ключа.

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.