带有Firebase的Android无法从Node.js Firebase应用接收通知

时间:2018-08-24 11:41:18

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

我有一个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());
    }
}

这是新的(附加的未替换的)代码,旨在接收主题消息:

@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上命中

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我发现的解决方案正在添加:

<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()。