Receive Push Notifications on Firebase database child added

Viewed 739

in my IOS application i have set up Firebase. I'am able to read, write and delete data. I also have setup Push Notifications and receive them from Firebase console.

What i did not get to work is to receive Push Notification when i add new data to my Firebase database.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    // Messaging.messaging().delegate = self
    Messaging.messaging().shouldEstablishDirectChannel = true       

    //Device Token for Push
    // iOS 10 support
    if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    }
        // iOS 7 support
    else {
        application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
    }
    return true
}

I try to subscribe to one of my database nodes but i get no Push Notification when something changes

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Convert token to string
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("APNs device token: \(deviceTokenString)")
    //Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.sandbox)
    Messaging.messaging().subscribe(toTopic: "/topics/news")

    // Persist it in your backend in case it's new
    UserDefaults.standard.set(deviceTokenString, forKey: "PushDeviceTokenString")
}
1 Answers
Related