简体   繁体   English

当应用程序在前台使用 Firebase 时不显示通知

[英]Notification not display when app in foreground using Firebase

我正在使用来自服务器的 firebase 推送通知,但是当应用程序在前台时,通知不显示请帮助我

Refer offcial link for complete reference.请参阅官方链接以获取完整参考。

If you intend on generating your own notifications as a result of a received FCM do something like this如果您打算根据收到的 FCM 生成自己的通知,请执行以下操作

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.

       sendNotification(remoteMessage.getNotification().getBody());// or pass value fetched from data payload
...
}

Then delcare Notification builder like this,然后像这样delcare Notification builder,

 private void sendNotification(String messageBody) {
        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);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序在前台时推送通知(使用 Firebase 触发器) - Push notification when app is in foreground (using Firebase triggers) 应用程序在前台时如何处理Firebase通知 - How to handle the Firebase notification when app is in foreground 当应用程序在前台使用 ZB12E1515C25C7D8C0352F1413AB9A15Z 通知 android - Notification is coming sometimes and sometimes not coming when the app is in foreground using firebase notifications in Flutter for android 在Android中-当应用程序处于前台时如何防止Firebase推送通知? - In android - How to prevent Firebase push notification when app is in foreground? 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 当应用程序在前台运行时,在警告框中显示推送通知 - Display push notification in alert box when the app is running in the foreground 即使app在前台,如何让FCM显示通知? - How to let FCM display notification even when app is in foreground? 当应用程序在前台时不显示通知 - Notification not showing when app is in foreground 如果应用程序在前台,如何停止 firebase 通知 - How to stop firebase notification if app is in foreground Android 在 Firebase 通知上将应用程序带到前台 - Android bring app to foreground on Firebase notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM