【问题标题】:How to handle the Firebase notification when app is in foreground当应用程序处于前台时如何处理 Firebase 通知
【发布时间】:2016-11-21 22:01:46
【问题描述】:

我已将 Firebase 云消息传递与我的应用程序集成。 当我从 Firebase 控制台发送通知时,如果应用程序在后台或未打开,我会成功收到通知, 否则,如果应用程序在前台或已打开,我没有收到它。

感谢所有建议。

【问题讨论】:

标签: android broadcastreceiver firebase-cloud-messaging


【解决方案1】:

当应用程序处于前台时,通知不会自行生成。您需要编写一些额外的代码。收到消息时,会调用 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());
    }
}

【讨论】:

  • 任何新通知都将替换之前的通知,因为您使用 0 作为 id
  • 我的安卓手机系统界面崩溃了。
  • 提示:您还应该添加检查 BUILD 版本 > O。否则通知将不会出现在模拟器中。我的虚拟设备是 NEXUS 5(在 Android O 中运行)。一开始我并没有意识到这一点,然后疯了几个小时。
  • 通知行为会根据应用是否打开而改变!有什么想法吗?
  • @JayPatel 在前台的情况下,您可以完全控制通知,因此您可以。轻松创建待处理的意图(如果您想在点击通知时打开启动器活动以外的活动)。但如果在点击通知时打开后台启动器活动。
【解决方案2】:

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 商店中将 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?你确定吗?
  • 是的,当应用处于前台时接收数据并显示通知
  • 如果我是正确的,C2DM 或 GCM 已完全弃用,不应使用。
  • 是的,这是真的
  • 似乎@only android 的答案足以在应用程序处于前台时检索并显示通知。
猜你喜欢
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2021-10-17
相关资源
最近更新 更多