【问题标题】:Android with Firebase not receiving notification from Node.js Firebase app带有 Firebase 的 Android 未收到来自 Node.js Firebase 应用程序的通知
【发布时间】:2019-01-30 21:21:18
【问题描述】:

我有一个成功接收来自 Firebase 控制台的通知的 Android 应用。我现在打算构建一个 nodejs 服务器,我们可以在其中发送这些通知以保存登录到 firebase 控制台,但是,node.js 库“firebase-admin”似乎只支持发送到单个设备 ID 或主题而不是所有设备根据控制台。

所以我创建了一个 nodejs 服务来发送到主题“all”,并尝试更改 android 以接收这些通知,但是我的设备上没有收到来自该 nodejs 服务器的通知。

这是我的服务器代码:

var admin = require("firebase-admin");

var serviceAccount = require("./firebase-privatekey.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://myapp-android-xxx.firebaseio.com"
});

var payload = {
    notification: {
        title: "Account Deposit",
        body: "A deposit to your savings account has just cleared."
    },
    data: {
        account: "Savings",
        balance: "$3020.25"
    },
    topic: "all",
};

admin.messaging().send(payload)
    .then(function(response) {
        console.log("Successfully sent message:", response);
    })
    .catch(function(error) {
        console.log("Error sending message:", error);
    });

这是与控制台通知一起使用的 android 代码:

public class MyNotificationService extends FirebaseMessagingService {
    public MyNotificationService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("Firebase", "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d("Firebase", "Message data payload: " + remoteMessage.getData());
            handleNow(remoteMessage.getData(), remoteMessage.getNotification().getBody());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d("Firebase", "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }

    public void handleNow(Map<String, String> data, String title) {
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        int notificationId = 1;
        String channelId = "channel-01";
        String channelName = "Channel Name";
        int importance = NotificationManager.IMPORTANCE_HIGH;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(
                    channelId, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
                .setSmallIcon(R.drawable.myapp_notification_icon)
                .setBadgeIconType(R.drawable.myapp_notification_icon)
                .setContentTitle(title)
                .setContentText(data.get("information"));


        notificationManager.notify(notificationId, mBuilder.build());
    }
}

这是新的(未替换的)代码,目的是接收主题消息:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //other code...
        FirebaseMessaging.getInstance().subscribeToTopic("all")
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            System.out.println("win");
                        } else {
                            System.out.println("fail");
                        }
                    }
                });

}

nodejs 服务器告诉我它成功发送了一条消息,但是在 android 上永远不会命中 win 或 fail 消息上的断点

【问题讨论】:

  • 如果您有兴趣,我已经在我的一个教程中逐步解释了如何使用Cloud FirestoreNode.jsnotifications 发送给特定用户.

标签: android node.js firebase firebase-cloud-messaging


【解决方案1】:

我也有同样的问题。我找到的解决方案是添加:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

AndroidManifest.xml 和:

@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
}

MyFirebaseMessagingService

根据docs.

覆盖 onDeletedMessages

在某些情况下,FCM 可能不会传递消息。这发生在 有太多消息 (>100) 等待您的应用在 特定设备在连接时或设备未连接时 在一个多月内连接到 FCM。在这些情况下,您可以 接收到 FirebaseMessagingService.onDeletedMessages() 的回调 当应用实例接收到这个回调时,它应该执行一个完整的 与您的应用服务器同步。如果您还没有向应用发送消息 该设备在过去 4 周内,FCM 不会调用 onDeletedMessages()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多