ios – 如何在应用启动后立即为用户订阅通知?

ios – 如何在应用启动后立即为用户订阅通知?,第1张

概述我正在使用HTTP POST向应用程序上的所有用户发送通知.为此,我有一个主题要订阅称为“全局”.我希望每个用户都自动订阅此主题,以确保他们收到通知(假设他们已启用通知). 在我的代码中应该放置订阅以确保它们将始终被订阅?我担心的是,我把它放在了错误的位置,而且它们永远不会被订阅.我试着在didFinishLaunchingWithOptions结束时订阅,但看起来现在这样做太早了(我想因为用户可 我正在使用http POST向应用程序上的所有用户发送通知.为此,我有一个主题要订阅称为“全局”.我希望每个用户都自动订阅此主题,以确保他们收到通知(假设他们已启用通知).

在我的代码中应该放置订阅以确保它们将始终被订阅?我担心的是,我把它放在了错误的位置,而且它们永远不会被订阅.我试着在dIDFinishLaunchingWithOptions结束时订阅,但看起来现在这样做太早了(我想因为用户可能还没有接受通知提示呢?).

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

@H_404_15@import UIKitimport Firebaseimport FirebaseMessagingimport UserNotifications@UIApplicationMainclass 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,.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.}解决方法 您需要在两个地方进行主题订阅.

一个就在FIRApp.configure()之后.还要更新Firebase iOS SDK版本.好像你有一个旧版本.

根据新版本(FirebaseMessaging 2.0.1).

@H_404_15@if Messaging.messaging().fcmToken != nil { Messaging.messaging().subscribe(totopic: “foo”)}

然后刷新令牌时的第二个.

@H_404_15@func messaging(_ messaging: Messaging,dIDRefreshRegistrationToken fcmToken: String) { Messaging.messaging().subscribe(totopic: “foo”)}

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

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

总结

以上是内存溢出为你收集整理的ios – 如何在应用启动后立即为用户订阅通知?全部内容,希望文章能够帮你解决ios – 如何在应用启动后立即为用户订阅通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存