尝试通过Firebase云端function发送通知(Android)后出现错误
我是Firebase和nodejs的新手。 我正尝试使用Firebase云端函数从一台设备向另一台设备发送通知。
这是发送通知的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); }) })
这是将通知添加到实时数据库以便触发该function的代码:
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) { } }); }
我可以看到该函数被执行,但有以下错误:
通知出现在数据库中:
这是function如何出现在控制台中:
问题是通知不发送。 我得到这个: {results:[{error: [Object]}]
出于某种原因。 什么可能是这个错误的原因?
编辑:(解决scheme)
正如在评论中所build议的,我已经使用了这个: 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}
答复真的很清楚,令牌已经改变了。 我已经将其更改为有效的令牌,它的工作。
正如在评论中所build议的,我已经使用了这个: 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}
答复真的很清楚,令牌已经改变了。 我已经将其更改为有效的令牌,它的工作。