您需要在后台运行的服务才能捕获PUSH通知并将其显示给用户,这就是Firebase通知服务为您提供的功能.我有这个(你必须修改它,以便在出现通知时做你想做的事):
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification().getBody() != null) { Log.e("FIREBASE", "Message Notification Body: " + remoteMessage.getNotification().getBody()); sendNotification(remoteMessage); } } private void sendNotification(RemoteMessage remoteMessage) { RemoteMessage.Notification notification = remoteMessage.getNotification(); 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) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.logo_gris)) .setSmallIcon(R.drawable.logo_gris) .setContentTitle(notification.getTitle()) .setContentText(notification.getBody()) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }