1

我正在使用HTTP POST將通知發送給應用上的所有用戶。要做到這一點,我有一個主題訂閱稱爲「全球」。我希望每個用戶都能自動訂閱此主題以確保他們獲得通知(假設他們已啓用通知)。如何在應用程序啓動後爲用戶訂閱通知的主題?

在我的代碼中,我應該將訂閱放在哪裏以確保它們始終會訂閱?我的恐懼是,我把它放在一個錯誤的地方,他們永遠不會被訂閱。我嘗試在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. 
} 

回答

3

您需要在兩個地方訂閱主題。

緊跟在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

0

井火力地堡消息4.0.4將它添加到didRegisterForRemoteNotificationsWithDeviceToken方法,這將完成剩下的

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     Messaging.messaging().apnsToken = deviceToken 
     Messaging.messaging().subscribe(toTopic: hubId) 
    } 
相關問題