【问题标题】:firestore cloud messaging iOS Swiftfirestore 云消息 iOS Swift
【发布时间】:2019-01-21 21:34:56
【问题描述】:

我无法在我的 firestore 文档数据库中获取新记录条目以向用户生成警报。

  1. IOS 应用可以毫无问题地获取和更新 Firestore 数据
  2. 如果我从 firebase 手动发送消息,我的应用会收到消息没有问题
  3. 我可以毫无错误地将我的云功能部署到 Firebase

我做错了什么?谢谢你的帮助。

let functions = require('firebase-functions')
let admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

let db = admin.firestore()

  exports.announceAlert = functions.database
    .ref('/alerts/{documentId}')
    .onCreate((snapshot, context) => {
        let alert = snapshot.val()
        sendNotification(alert)
    })

function sendNotification(alert) {

    let title = alert.Incident_Number
    let summary = alert.Flash_Summary
    let status = alert.Incident_Status  


    let payload = {
        notification: {
            title: 'Critical Incident: ' + title, 
            body: 'Summary: ' + summary,
            sound: 'default' 

        }
    }

    console.log(payload)

    let topic = "Alerts"
    admin.messaging().sendToTopic(topic, payload) 

}

【问题讨论】:

    标签: ios swift google-cloud-firestore firebase-cloud-messaging


    【解决方案1】:

    这就是我所做的,并且奏效了。请记住,您的 iOS 应用程序的用户必须订阅该主题,而您可以通过该应用程序进行订阅。下面的代码只是一个函数,告诉 firebase 在某个存储库中创建新文档时向订阅用户发送通知。

    let functions = require('firebase-functions')
    let admin = require('firebase-admin')
    admin.initializeApp(functions.config().firebase)
    
    let db = admin.firestore()
      exports.announceMessage = functions.firestore
        .document('/myData/{documentId}')
        .onCreate((snapshot, context) => {
            let message = snapshot.data()
            sendNotification(message)
        })
    
    function sendNotification(message) {
    
        let title = message.column1  
        let notification = message.column2
    
        let payload = {
            notification: {
                title: 'Some title: ' + title, 
                body: 'Some header: ' + notification
    
            },
    
    
        }
    
    console.log(payload)
    let topic = "yourTopic"
    return admin.messaging().sendToTopic(topic, payload)
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2019-07-12
      • 2020-07-10
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多