我正在使用firebase函数和firestore发送推送通知。更改Firestore数据库时,该功能运行良好,甚至说该消息已成功发送。但是我的设备在后台或前台都没有收到该消息。
我已确保注册令牌正确。 用于生产和开发的.p12都包含在项目中。
在didfinishlaunchingwithoptions函数中
Messaging.messaging().delegate = self
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
let token = result.token
let db = Firestore.firestore()
if let driverID = UserDefaults.standard.string(forKey: "driverID"){
db.collection("drivers").document(driverID).updateData(["registrationToken":token])
}
}
}
和didreceiveregistrationtoken委托方法
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
let db = Firestore.firestore()
if let driverID = UserDefaults.standard.string(forKey: "driverID"){
db.collection("drivers").document(driverID).updateData(["registrationToken":fcmToken])
}
}
最后将功能部署到Firebase
出于调试目的,我复制粘贴了令牌,而不是从数据库中获取令牌。
exports.sendDispatchedNotification = functions.firestore.document('/dispatching/{cityID}/{dateID}/trips/uncomplete/{tripID}')
.onUpdate(async (change) => {
const payload = {
notification: {
title: 'Trip Ready',
body: 'There is a trip ready for you to start.'
}
}
admin.messaging().sendToDevice("TOKEN", payload)
.then(response => {
console.log("successfully sent message:", response)
return
})
.catch(error => {
console.log("Error sending message:", error);
})
})
它记录“成功发送的消息:”很好,但是我的设备没有收到消息。