Firebase topic notifications

I have a firebase DB set up with a topic so each time a user pushes a new message to the DB it sends a push notification to the users device

For some reason it is sending the notification to all devices.. I cannot figure out why its doing this :frowning:

Here is the function I've set up on firebase, And also my AppDelegate where it receives the push notification:

const functions = require('firebase-functions');

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

exports.sendNotification = functions.database
   .ref('/messages/{uid}/{pushId}')
   .onWrite( event => {

   const newMessage = event.data.val();

   console.log(newMessage.message);

   const payload = {
   notification: {
       title: 'Loop',
       body: newMessage.name + ' has interacted with his device',
       sound: 'default'
   },
   data: {
       name: newMessage.name,
       message: newMessage.message
   }
   };

   const options = {
   priority: "high",
   timeToLive: 60 * 60 * 24 //24 hours
   };

   return admin.messaging().sendToTopic("notifications", payload, options);
});

And heres the App Delegate:

    import UIKit
import UserNotifications
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    completionHandler(UIBackgroundFetchResult.newData)
}

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    Messaging.messaging().subscribe(toTopic: "notifications")
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().subscribe(toTopic: "notifications")
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    self.window = UIWindow(frame: UIScreen.main.bounds)
    Utilities.decideInitialViewController(window: self.window!)
    application.registerForRemoteNotifications()
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
}

func applicationDidEnterBackground(_ application: UIApplication) {
}

func applicationWillEnterForeground(_ application: UIApplication) {
}

func applicationDidBecomeActive(_ application: UIApplication) {
}

func applicationWillTerminate(_ application: UIApplication) {
}

}

I'm always happy to help, so I googled the documentation. sendToTopic sounds like a function that sends a message to everyone that joined a topic. Maybe you need sendToDevice...


Nevertheless, please consider this forum is about the language itself, the compiler, the core libs and closely related tools. You are always welcome with questions about development with Swift in the Apple Developer Forums and programming in general in Stack Overflow.

@forum_admins Would be convenient to make this eye catching for new members.