嗨iam尝试实施google firebase推送通知并订阅主题。
我能够接收推送通知但不能订阅topic.Error说Cannot subscribe to topic: 000000005eb68872 with token: (null)
但我已经生成了令牌。
我正在使用IOS 11和Xcode 9.0
Pod文件
target 'VQ Smart Home' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for VQ Smart Home
pod 'SwiftyJSON'
pod 'NVActivityIndicatorView'
pod 'Firebase/Core', '4.0.4'
pod 'Firebase/Database', '4.0.4'
pod 'Firebase/Messaging', '4.0.4'
pod 'FirebaseInstanceID', '2.0.0'
end
的AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().isTranslucent = true
/************************ FireBase Notification ************************************************************************/
FirebaseApp.configure()
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
let token = Messaging.messaging().fcmToken
print("********* FCM token: \(token ?? "") *************")
hubId = (isKeyPresentInUserDefaults(key: "hubID")) ? (UserDefaults.standard.value(forKey: "hubID") as? String)! : "NohubId"
Messaging.messaging().subscribe(toTopic: hubId)
/************************ FireBase Notification ************************************************************************/
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
// Push Notification Methods
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print(userInfo)
let aps = userInfo["aps"] as! NSDictionary
print(aps["alert"] as? String ?? "")
completionHandler(UIBackgroundFetchResult.newData)
}
错误堆栈
[1022:258833] [Firebase/Analytics][I-ACS003016] Firebase Analytics App Delegate Proxy is disabled. To log deep link campaigns manually,
调用FIRAnalytics + AppDelegate.h中的方法。 [1022] [Firebase / Analytics] [I-ACS003016] Firebase Analytics App代理代理已禁用。记录深层链接广告系列 手动,调用FIRAnalytics + AppDelegate.h中的方法。 ********* FCM令牌:dLdjXagKJpA:APA91bH500MivUUFRUl-1mgqn7qrzp6lVG2divijtBR9RYBgQu4slIBE8W7FN_3VtM_lbyE7S-xcgK40dzrlw2h4fz0SzDrfyPF90KIrUUqE9qNDwOGGrtOLemV1qC1wcrdL2L4aB1Ro
[1022:258837] [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension
长度小于sct数据长度 [1022:258810] [MC]延迟加载NSBundle MobileCoreServices.framework [1022:258810] [MC]已加载MobileCoreServices.framework [1022:258838] [Firebase / Messaging] [I-FCM002010]无法订阅主题:000000005eb68872 with token:(null) [1022:258810] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是 /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles ] [Firebase / Messaging] [I-FCM002010]无法订阅主题:000000005eb68872 with token:(null) [1022:258838] [Firebase / Analytics] [I-ACS005000] AdSupport Framework目前尚未关联。某些功能无法使用 正常。在http://了解更多信息 ] [Firebase / Analytics] [I-ACS005000] AdSupport Framework目前尚未关联。某些功能无法使用 正常。在http://了解更多信息 [1022:258810] refreshPreferences:HangTracerEnabled:0 [1022:258810] refreshPreferences:HangTracerDuration:500 [1022:258810] refreshPreferences:ActivationLoggingEnabled:0 ActivationLoggingTaskedOffByDA:0 [1022:258832] [Firebase / Analytics] [I-ACS023007] Firebase Analytics v.40002000已启动 ] [Firebase / Analytics] [I-ACS023007] Firebase Analytics v.40002000已启动 [1022:258832] [Firebase / Analytics] [I-ACS023008]要启用调试日志记录,请设置以下应用程序参数: -FIRAnalyticsDebugEnabled(参见参考资料) ] [Firebase / Analytics] [I-ACS023008]要启用调试日志记录,请设置以下应用程序参数: -FIRAnalyticsDebugEnabled(参见参考资料) [1022:258839] [Firebase / Analytics] [I-ACS032003] iAd框架未链接。搜索广告归因记者已停用。 ] [Firebase / Analytics] [I-ACS032003] iAd框架未链接。搜索广告归因记者已停用。 [1022:258839] [Firebase / Analytics] [I-ACS023012]已启用Firebase Analytics ] [Firebase / Analytics] [I-ACS023012]已启用Firebase Analytics [1022:258833] TIC读取状态[1:0x0]:1:57 [1022:258833] TIC读取状态[1:0x0]:1:57
有人可以帮我解决这个问题..
答案 0 :(得分:0)
找到解决方案
我试图在 didFinishLaunchingWithOptions 中订阅主题,但在此期间未加载所有require方法和模块。所以我将订阅代码移到 didRegisterForRemoteNotificationsWithDeviceToken 中,现在它可以正常工作。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
Messaging.messaging().subscribe(toTopic: hubId)
}