Ich verwende HTTP POST, um Benachrichtigungen an alle Benutzer in der App zu senden. Um dies zu tun, habe ich ein Thema zu abonnieren namens "global". Ich möchte, dass jeder Benutzer dieses Thema automatisch abonniert, um sicherzustellen, dass er die Benachrichtigungen erhält (vorausgesetzt, die Benachrichtigungen sind aktiviert).Wie kann ich Benutzer für ein Thema für Benachrichtigungen abonnieren, sobald die App gestartet wird?
Wo in meinem Code sollte ich das Abonnement setzen, um sicherzustellen, dass sie immer abonniert werden? Meine Angst ist, dass ich es falsch darstelle und sie nie am Ende abonniert werden. Ich habe versucht, mich am Ende von didFinishLaunchingWithOptions anzumelden, aber es sieht so aus, als sei es zu früh, um es hier zu tun (ich nehme an, da der Benutzer die Benachrichtigungsaufforderung noch nicht akzeptiert hat?).
Derzeit ist das Abonnement in didRegisterForRemoteNotificationsWithDeviceToken jedoch ist dies nicht auf dem ersten App Laufe aufgerufen so für sie mich die App ein zweites Mal ausführen muß, um zu arbeiten ... Hier ist mein entsprechender Code in 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.
}