应用程序启动后,如何为用户订阅主题以获取通知?

Posted

技术标签:

【中文标题】应用程序启动后,如何为用户订阅主题以获取通知?【英文标题】:How can I subscribe user to a topic for notifications as soon as app starts up? 【发布时间】:2017-05-23 23:56:22 【问题描述】:

我正在使用 HTTP POST 向应用程序上的所有用户发送通知。为此,我需要订阅一个名为“global”的主题。我希望每个用户都自动订阅此主题,以确保他们收到通知(假设他们已打开通知)。

我应该将订阅放在我的代码中的哪个位置,以确保它们始终被订阅?我担心我把它放错了地方,他们永远不会被订阅。我试图在 didFinishLaunchingWithOptions 结束时订阅,但现在在这里订阅似乎还为时过早(我猜是因为用户可能还没有接受通知提示?)。

目前订阅在 didRegisterForRemoteNotificationsWithDeviceToken 中,但是在第一个应用程序运行时不会调用它,所以为了让它工作,我必须第二次运行应用程序......这是我在 AppDelegate 中的相关代码:

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate 

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

    FIRApp.configure()

    if #available(ios 10.0, *) 
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: _, _ in )

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = self

     else 
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    

    application.registerForRemoteNotifications()

    return true




func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) 
    print("applicationReceivedRemoteMessage")




func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
    if let refreshedToken = FIRInstanceID.instanceID().token() 
        print("InstanceID token: \(refreshedToken)")
        FIRMessaging.messaging().subscribe(toTopic: "/topics/global")
    




@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
    completionHandler(UNNotificationPresentationOptions.alert)




func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) 
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.




func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.

【问题讨论】:

【参考方案1】:

您需要在两个地方订阅主题。

FIRApp.configure() 之后的一个。同时更新 Firebase iOS SDK 版本。好像您使用的是旧版本。

根据新版本 (FirebaseMessaging 2.0.1)。

if Messaging.messaging().fcmToken != nil 
    Messaging.messaging().subscribe(toTopic: “foo”)

然后是刷新令牌时的第二个。

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) 
    Messaging.messaging().subscribe(toTopic: “foo”)

这是原始答案的链接。检查贡献者提供的答案。

https://github.com/firebase/quickstart-ios/issues/307

【讨论】:

【参考方案2】:

在 Firebase Messaging 4.0.4 中将其添加到 didRegisterForRemoteNotificationsWithDeviceToken 方法中,剩下的工作就完成了

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
        Messaging.messaging().apnsToken = deviceToken
        Messaging.messaging().subscribe(toTopic: hubId)
    

【讨论】:

【参考方案3】:

在这个didReceiveRegistrationToken方法中应该增加订阅方法,这样每当token刷新时,就会自动订阅主题。

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) 

Messaging.messaging().subscribe(toTopic: "TestTopic") error in
            if error == nil
                print("Subscribed to topic")
            
            else
                print("Not Subscribed to topic")
            
        

【讨论】:

以上是关于应用程序启动后,如何为用户订阅主题以获取通知?的主要内容,如果未能解决你的问题,请参考以下文章

如何为应用用户注册/订阅 OneSignal 中的主题?

如何为特定令牌设置静默通知?

如何为特定用户发送浏览器通知?

您如何向从应用程序本身订阅主题的用户发送 Firebase 推送通知

从 Lambda 向订阅主题的所有用户发送 SNS 通知

Vs Blend + Windows 8:如何为应用程序中的元素设置系统通知主题的样式?