带有Firebase的Android未收到Node.js Firebase应用程序的通知

带有Firebase的Android未收到Node.js Firebase应用程序的通知,android,node.js,firebase,firebase-cloud-messaging,Android,Node.js,Firebase,Firebase Cloud Messaging,我有一个android应用程序,它成功地从Firebase控制台接收通知。我现在打算构建一个nodejs服务器,在那里我们可以发送这些通知以保存登录firebase控制台的记录,但是,node.js库“firebase admin”似乎只支持发送到单个设备ID或主题,而不支持根据控制台发送到所有设备 所以我做了一个nodejs服务发送到主题“all”,并试图改变android来接收这些通知,但是我的设备上没有收到来自这个nodejs服务器的通知 这是我的服务器代码: var admin = re

我有一个android应用程序,它成功地从Firebase控制台接收通知。我现在打算构建一个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());
    }
}
公共类MyNotificationService扩展了FirebaseMessagingService{
公共MyNotificationService(){
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
Log.d(“Firebase”,“From:”+remoteMessage.getFrom());
//检查消息是否包含数据有效负载。
如果(remoteMessage.getData().size()>0){
Log.d(“Firebase”,“消息数据负载:”+remoteMessage.getData());
handleNow(remoteMessage.getData(),remoteMessage.getNotification().getBody());
}
//检查消息是否包含通知负载。
if(remoteMessage.getNotification()!=null){
Log.d(“Firebase”,“消息通知正文:”+remoteMessage.getNotification().getBody());
}
}
public void handleNow(地图数据、字符串标题){
NotificationManager NotificationManager=(NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_服务);
int notificationId=1;
字符串channelId=“channel-01”;
字符串channelName=“频道名称”;
int重要性=NotificationManager.importance\u HIGH;
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.O){
NotificationChannel mChannel=新NotificationChannel(
channelId、channelName、重要性);
notificationManager.createNotificationChannel(MCChannel);
}
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(getApplicationContext(),channelId)
.setSmallIcon(R.drawable.myapp_通知_图标)
.setBadgeIconType(R.drawable.myapp\u通知\u图标)
.setContentTitle(标题)
.setContentText(data.get(“信息”));
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");
                        }
                    }
                });

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
//其他代码。。。
FirebaseMessaging.getInstance().subscribeToTopic(“全部”)
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
System.out.println(“win”);
}否则{
系统输出打印项次(“失败”);
}
}
});
}

nodejs服务器告诉我这是一个成功的消息发送,但是在android上,win或fail消息上的断点从未被击中,我也遇到了同样的问题。我找到的解决方案是添加:

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

据报道

覆盖onDeletedMessages

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


如果您感兴趣,我已经在我的一个教程中一步一步地讲解了如何使用
Cloud Firestore
Node.js
向特定用户发送信息。
@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
}