【发布时间】:2018-01-19 01:19:56
【问题描述】:
通过 Cloud Functions for Firebase 发送成功通知后,通知不会在 ios 设备上显示为推送通知。我发现了几个类似的问题,但没有一个提供明确的解决方案。
云功能:
exports.sendInitialNotification = functions.database.ref('branches/{branchId}/notifications/{notifId}').onWrite(event => {
const data = event.data.val()
if (data.finished) return
const tokens = []
const notifId = event.params.notifId
const getPayload = admin.database().ref(`notifications/${notifId}`).once('value').then(snapshot => {
const notif = snapshot.val()
const payload = {
notification: {
title: notif.title,
body: notif.message,
},
data: {
'title': notif.title,
'message': notif.message,
'id': String(notif.id),
}
}
if (notif.actions) {
payload.data['actions'] = JSON.stringify(notif.actions)
}
console.log('payload:', payload)
return payload
}, (error) => {
console.log('error at sendInitialNotification getPayload():', error)
})
const getTokens = admin.database().ref(`notifications/${notifId}/users`).once('value').then(snapshot => {
const users = snapshot.forEach((data) => {
let promise = admin.database().ref(`users/${data.key}/profile/deviceToken`).once('value').then(snap => {
if (tokens.indexOf(snap.val()) !== -1 || !snap.val()) return
return snap.val()
}, (error) => {
console.log('error retrieving tokens:', error)
})
tokens.push(promise)
})
return Promise.all(tokens)
}, (error) => {
console.log('error at sendInitialNotification getTokens()', error)
}).then((values) => {
console.log('tokens:', values)
return values
})
return Promise.all([getTokens, getPayload]).then(results => {
const tokens = results[0]
const payload = results[1]
if (payload.actions) {
payload.actions = JSON.stringify(payload.actions)
}
const options = {
priority: "high",
}
admin.messaging().sendToDevice(tokens, payload, options)
.then(response => {
data.finished = true
admin.database().ref(`notifications/${notifId}`).update({success: true, successCount: response.successCount})
console.log('successfully sent message', response)
}).catch(error => {
data.finished = true
admin.database().ref(`notifications/${notifId}`).update({success: false, error: error})
console.log('error sending message:', error)
})
})
})
...以及 firebase 控制台中的日志:
成功发送消息{结果:[{错误:[对象]}],
canonicalRegistrationTokenCount:0,失败计数:1,成功计数: 0、multicastId: 7961281827678412000 }代币:[ 'eS_Gv0FrMC4:APA91bEBk7P1lz...']
有效负载:{通知:{标题:'test07',正文:'test07'},数据: { 标题:'test07',消息:'test07',id:'1502383526361' } }
...但是 iphone 上没有显示通知。我确定我在这里的 OOO(操作顺序)中遗漏了一些东西,但不确定障碍在哪里。如果有人能指出我的缺陷,请随时公开批评。
一如既往地提前感谢您,感谢您的任何指导!
【问题讨论】:
-
感谢您的管家服务...在进一步调查中,这似乎与越来越流行的说法有关:“无效的 APNs 证书。检查设置中的证书”。我已按照文档中的指示通过 APNs 身份验证密钥创建并上传了一个新密钥,但在每次发送的通知中仍然看到错误...不知所措!
-
嗨工作室大脑。您是否尝试过通过Postman 发送模拟测试通知并查看它是否可以从那里工作?它可能会给你一个更好的错误信息。
-
我有。我看到的问题是由于 xcode 中的反向域 id 与 firebase 中的不匹配。它现在正在工作。
-
酷。我建议尽可能多地添加答案。有一天它可能会帮助遇到类似问题的人。干杯! :)
-
大多数情况下,这个问题似乎是由于配置不当造成的,例如捆绑名称中的拼写错误、App ID 中的错误权利等。但是,我发现如果其他方法都失败了,只有一种解决方案是在 Firebase Cloud 设置中使用您的团队 ID 而不是应用前缀。这里不是答案,但这可能对其他人有帮助。
标签: ios firebase firebase-cloud-messaging google-cloud-functions