gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:38:30 24 4
gpt4 key购买 nike

我有一个 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 上永远不会命中成功或失败消息上的断点

最佳答案

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

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

Override onDeletedMessages

In some situations, FCM may not deliver a message. This occurs when there are too many messages (>100) pending for your app on a particular device at the time it connects or if the device hasn't connected to FCM in more than one month. In these cases, you may receive a callback to FirebaseMessagingService.onDeletedMessages() When the app instance receives this callback, it should perform a full sync with your app server. If you haven't sent a message to the app on that device within the last 4 weeks, FCM won't call onDeletedMessages().

关于带有 Firebase 的 Android 没有收到来自 Node.js Firebase 应用程序的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com