Ho problemi con le notifiche push di FireBase. Quando la mia app è in background, arrivano le notifiche, ma quando la mia app è in primo piano non ricevo le notifiche, ma nella console viene visualizzata la notifica, il che significa che la notifica è qui ma non viene visualizzata nella barra delle notifiche . Potresti aiutarmi?Notifiche Firebase in primo piano
@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());
}
Sì, quello era un problema, grazie amico. Solo un'altra domanda, chi impostare setContentTitle() quando il mio backend mi invia una notifica e in json set title? –
Le notifiche visualizzate tramite un messaggio di notifica (json), sono completamente separate dalla creazione e visualizzazione della notifica nel codice come si fa qui. Se si utilizza un messaggio di notifica e l'app è in background, verrà visualizzata in base al json specificato. Altrimenti riceverai la richiamata onMessageReceived e spetterà al tuo codice mostrare una notifica se è quello che vuoi che succeda. –