【发布时间】: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 Firestore和Node.js将notifications 发送给特定用户.
标签: android node.js firebase firebase-cloud-messaging