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');

exports.sendNotification = functions.database
   .onWrite( event => {

   const newMessage =;


   const payload = {
   notification: {
       title: 'Loop',
       body: + ' has interacted with his device',
       sound: 'default'
   data: {
       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
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

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

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 {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    Utilities.decideInitialViewController(window: self.window!)
    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.