简体   繁体   English

应用程序在前台时如何处理Firebase通知

[英]How to handle the Firebase notification when app is in foreground

I have integrated Firebase Cloud Messaging with my application. 我已将Firebase Cloud Messaging与我的应用程序集成在一起。 When I sent a notification from the Firebase console, if the app is in background or not opened I receive successfully the notification, otherwise if the app is in foreground or opened, I did not receive it. 当我从Firebase控制台发送通知时,如果该应用程序在后台或未打开,则我会成功收到该通知,否则,如果该应用程序在前台或已打开,则不会收到该通知。

All suggestions are appreciated. 所有建议表示赞赏。

When app is in foreground, notifications are not generated themselves. 当应用程序处于前台时,不会自行生成通知。 You need to write some additional code. 您需要编写一些其他代码。 When message is received onMessageReceived() method is called where you can generate the notification. 收到消息后,将调用onMessageReceived()方法,您可以在其中生成通知。 Here is the code: 这是代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        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);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        manager.notify(0, builder.build());
    }
}

FirebaseMessagingService never work when the app is in foreground. 当应用程序处于前台状态时, FirebaseMessagingService永远无法工作。 In this case if you want to receive the message from FCM then WakefulBroadcastReceiver will work for you 在这种情况下,如果您想从FCM接收消息,则WakefulBroadcastReceiver将为您工作

public class FirebaseDataReceiver extends WakefulBroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {

            Log.d("BroadcastReceiver::", "BroadcastReceiver");
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(intent.getExtras().getString("title"))
                    .setContentText(intent.getExtras().getString("message"));
            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            manager.notify(0, builder.build());
        }
    }

Link firebase with GCM in play store and write the below code in manifest firebase与Play商店中的GCM链接,并在清单中编写以下代码

<receiver
    android:name=".firebase.FirebaseDataReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</receiver>

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

相关问题 如何在后台和前台处理 firebase 通知? - How to handle firebase notification on background as well as foreground? 在Android中-当应用程序处于前台时如何防止Firebase推送通知? - In android - How to prevent Firebase push notification when app is in foreground? Android-在前台运行应用程序时处理本地通知 - Android - Handle local notification when app in foreground Firebase app在后台时如何处理通知 - How to handle notification when app in background in Firebase 当应用程序在前台使用 Firebase 时不显示通知 - Notification not display when app in foreground using Firebase 如果应用程序在前台,如何停止 firebase 通知 - How to stop firebase notification if app is in foreground Android Firebase:当应用程序处于前台时如何从Firebase获取通知消息的消息文本组件 - Android Firebase : how get the message text component of notification message from firebase when app is in foreground 应用程序在前台时推送通知(使用 Firebase 触发器) - Push notification when app is in foreground (using Firebase triggers) 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 当应用程序处于前台状态时如何在通知栏中显示通知 - how to show notification to notification tray when app is in foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM