Я отправляю полезную нагрузку DATA от Почтальона, поэтому уведомление должно обрабатываться в onMessageReceived независимо от того, находится ли приложение на переднем или заднем плане.
Согласно моим журналам и тестированию, onMessageReceived запускается, но уведомленияне отображается (не зависит, находится ли приложение на переднем или заднем плане).
в манифесте у меня есть:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service> <!-- [END firebase_iid_service] -->
<service
android:name=".MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
, а вот MyFirebaseMessagingService.java:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title = "";
if (Objects.requireNonNull(remoteMessage.getData()).get("title") != null)
title = remoteMessage.getData().get("title");
String message = "";
if (Objects.requireNonNull(remoteMessage.getData()).get("message") != null)
message = remoteMessage.getData().get("message");
Log.e("notification",title);
sendNotification(title, message);
}
private void sendNotification(String title, String body) {
Intent i = new Intent(this, Novinky.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this,
0, // Request code
i,
PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_notify)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);
NotificationManager manager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.notify(0, builder.build());
}
}
Итак, у меня есть Log.e («уведомление», заголовок);там, и он выполняется должным образом, но уведомление не получено.
Прежде чем я отправлю также уведомление + данные через Почтальон, это работало, но это только автоматическое уведомление, не обработанное приложением, так что теперь яхочу отправлять только полезные данные DATA и обрабатывать их в приложении, но по какой-то причине это не работает.