Mam problem z powiadomieniami push w FireBase. Kiedy moja aplikacja jest w tle, nadchodzą powiadomienia, ale gdy moja aplikacja jest na pierwszym planie, nie otrzymuję powiadomień, ale w konsoli wyświetlane jest powiadomienie, co oznacza, że powiadomienie jest tutaj, ale nie jest wyświetlane na pasku powiadomień . Czy mógłbyś mi pomóc?Powiadomienia Firebase na pierwszym planie
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("OnMessage", "Received");
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
String aa=remoteMessage.toString();
Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
intent.putExtra("msg", aa);
sendNotification(remoteMessage);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentText(remoteMessage.getNotification().getBody());
// remoteMessage.getNotification().getBody();
}
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Tak, to był problem, dziękuję kolego. Kolejne pytanie, kto ustawić setContentTitle(), gdy mój backend przesyła mi powiadomienie, a json ustawia tytuł? –
Powiadomienia wyświetlane za pomocą powiadomienia (json), są całkowicie niezależne od tworzenia i wyświetlania powiadomień w kodzie takim jak tutaj. Jeśli użyjesz wiadomości z powiadomieniem, a Twoja aplikacja będzie w tle, wyświetli się ona z podanym jsonem. W przeciwnym razie otrzymasz oddzwanianie onMessageReceived. Od tego zależy Twój kod, aby wyświetlić powiadomienie, jeśli tak właśnie chcesz. –