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

41

我已将 Firebase 云消息传递集成到我的应用程序中。当我从 Firebase 控制台发送通知时,如果应用程序在后台或未打开,则成功接收到通知, 但如果应用程序在前台或已打开,则无法接收到通知。

欢迎提出所有建议。


1
请点击此链接,这是处理 Firebase 通知的最简单方法,当应用程序在前台和后台时,使用 Postman 而不是服务器。Android 中的 Firebase 后台和前台通知 - Abhinay Shrivastav
2个回答

98

当应用程序在前台时,通知本身不会生成。您需要编写一些额外的代码。当接收到消息时,将调用onMessageReceived()方法,您可以在其中生成通知。以下是代码:

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());
    }
}

2
由于您使用0作为ID,因此任何新通知都将替换先前的通知。 - Louis CAD
2
崩溃了我的安卓手机的系统界面。 - Travis Whitten
提示:您还应添加一个检查版本> O的构建版本。否则,通知将不会出现在模拟器中。我的虚拟设备是NEXUS 5(运行在Android O上)。我一开始没有意识到这一点,结果疯狂了几个小时。 - Rohit Singh
1
通知行为会根据应用程序是否打开而改变!有什么想法吗? - Jay Patel
@JayPatel 在前台情况下,您可以完全控制通知,因此您可以轻松创建挂起意图(如果您想在单击通知时打开其他活动而不是启动器活动)。但是,在后台情况下,单击通知会打开启动器活动。 - Shivam Bhusri
对我来说,令人困惑的是 Firebase 没有提供一个更容易将 RemoteMessage.Notification 对象转换为系统通知的帮助程序。这实际上是他们在后台通知中幕后所做的事情,我只希望我们可以重用代码而不是重复一切... - David Ferrand

5
当应用程序在前台运行时,FirebaseMessagingService 不起作用。在这种情况下,如果您想从 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());
        }
    }

在 play store 中将 firebaseGCM 连接,并在清单中编写以下代码:

<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>

C2DM?你确定吗? - wonsuc
是的,在应用程序处于前台时接收数据并显示通知。 - Ramana V V K
1
如果我没记错的话,C2DM或GCM已经完全被弃用,不应该再使用。 - wonsuc
是的,那是正确的。 - Ramana V V K
看起来@only android的回答已经足够在应用程序在前台时检索和显示通知。 - wonsuc
不,这是一个特殊情况,我们已经使用这个解决方案解决了旧版Firebase中之前未触发Firebase消息接收器的问题。 - Ramana V V K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接