1
我是Firebase和nodejs的新手。我正嘗試使用Firebase雲端函數從一臺設備向另一臺設備發送通知。嘗試通過Firebase雲端功能發送通知後發生錯誤(Android)
這是發送通知的代碼的node.js:
var functions = require('firebase-functions');
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/sendNotification/{notificationId}')
.onWrite(event => {
var regToken="fYRweeL8cic:APA91bH6Q_gyKKrLL...";
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = event.data;
var payload = {
data: {
title: eventSnapshot.child("title").val()
}
};
// Set the message as high priority and have it expire after 24 hours.
var options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
admin.messaging().sendToDevice(regToken,payload,options)
.then(function(response){
console.log("Successfully sent message: ", response);
})
.catch(function(error){
console.log("Error sending message: ", error);
})
})
這是爲了將所述通知發送到實時數據庫觸發函數的代碼:
public void sendNotification(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("sendNotification");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getApplicationContext(),
"sent", Toast.LENGTH_SHORT).show();
Map data = new HashMap();
data.put("title", "this is my title");
data.put("message", "this is the message");
myRef.push().setValue(data);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我可以看到該功能已執行,但出現以下錯誤:
這是函數是如何出現在控制檯:
的問題是,不發送通知。 我得到這個:{results:[{error: [Object]}]
出於某種原因。 這個錯誤的原因是什麼?
編輯:(解決方案)
正如評論所說,我已經使用這個:JSON.stringify(response)
得到一些更多的信息。這是迴應:
{"results":[{"error":{"code":"messaging/registration-token-not-registered","message":"The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":6051985890611026000}
響應非常明確,令牌已經改變。我已將其更改爲有效的令牌,並且工作正常。
最後一行不是錯誤,而是警告:https://stackoverflow.com/questions/42784135/cloud-functions-for-firebase-billing-account-not-configured。我不確定是什麼導致失敗。 –
@FrankvanPuffelen我編輯了我的文章。還有更多有用的信息可以添加嗎? –
你可以試一下'console.log(「成功發送消息:」,JSON.stringify(response));'獲取更多細節? –