【问题标题】:Receive Push Notifications on Firebase database child added在添加的 Firebase 数据库子节点上接收推送通知
【发布时间】:2023-03-07 10:37:02
【问题描述】:

在我的 IOS 应用程序中,我设置了 Firebase。 我能够读取、写入和删除数据。 我还设置了推送通知并从 Firebase 控制台接收它们。

当我将新数据添加到我的 Firebase 数据库时,我没有开始工作的是接收推送通知。

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
}

我尝试订阅我的一个数据库节点,但是当发生变化时我没有收到推送通知

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")
}

【问题讨论】:

  • 我觉得你应该看看Cloud Functions for Firebase
  • 好的,我明白了。我通过 npm 和 node .js 安装了 firebase 工具。下一步是运行 firebase login。但我的终端说 firebase: command not found
  • 我把一切都搞定了。谢谢。
  • 酷。我建议在下面为您自己的帖子添加一个答案。其他人可能会发现它在未来很有用。干杯! :)

标签: ios firebase push-notification firebase-cloud-messaging subscribe


【解决方案1】:

在我根据 firebase 指南在我的项目中设置了 firebase 功能之后。

我所要做的就是创建和部署一个服务器端函数,该函数捕获一个事件并执行所需的功能。

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

//register to onWrite event of my node news
exports.sendPushNotification = functions.database.ref('/news/{id}').onWrite(event => {
    //get the snapshot of the written data
    const snapshot = event.data;
    //create a notofication
    const payload = {
        notification: {
            title: snapshot.child("title").val(),
            body: snapshot.child("message").val(),
            badge: '1',
            sound: 'default',
        }
    };

    //send a notification to all fcmToken that are registered
    //In my case the users device token are stored in a node called 'fcmToken'
    //and all user of my app will receive the notification
    return admin.database().ref('fcmToken').once('value').then(allToken => {
        if (allToken.val()){
            const token = Object.keys(allToken.val());
            return admin.messaging().sendToDevice(token, payload).then(response => {
                console.log("Successfully sent message:", response);
            });
        }
    });
});

【讨论】:

    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 2018-02-27
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2016-10-15
    相关资源
    最近更新 更多