【发布时间】: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