0

I have built an app using SwiftUI 2.0 that just houses a few documents stored inside a Firebase Firestore database. I have also integrated push notifications, so that you can manually send them through the cloud messaging service firebase offer.

My query is, is it possible to send a notification to all devices with the app downloaded, when a new document is added; alerting them to the new document. I have no experience with node.js which I think is what is used most of the time, but am wondering if it is possible to do it all within Swift.

My ideal situation would be to run the below function and have a notification sent out with the corresponding message!

func add(_ card: Card) {
    do {
        _ = try store.collection(path).addDocument(from: card)

        //Something here to send a notification to all other devices?!!

    } catch {
      fatalError("Unable to add card: \(error.localizedDescription).")
    }
  }
benpomeroy9
  • 375
  • 5
  • 21
  • 1
    Rather than trying to push from the device like this, it’s probably better to subscribe all of the devices to a certain channel and then push the message from Firebase Cloud Functions – jnpdx Sep 26 '21 at 21:58
  • It is not possible to securely send messages directly from one device to another. The reason for this is that calling the FCM API to *send* a message requires that you pass the FCM server key. As its name implies this key should only be used in trusted environments, such as your development machine, a server you control, and Cloud Function. If you were to use the FCM server key in the app, a malicious user can grab it from there and use it to send whatever message they want to all your users. For more on this see: http://stackoverflow.com/q/37634046 – Frank van Puffelen Sep 27 '21 at 00:02

0 Answers0