【发布时间】:2016-09-24 07:41:51
【问题描述】:
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter
.defaultCenter()
.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
// Override point for customization after application launch.
return true
}
func registerForPushNotifications(application: UIApplication) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("===== didReceiveRemoteNotification ===== %@", userInfo)
}
func tokenRefreshNotificaiton(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
同样在 Info.plist FirebaseAppDelegateProxyEnabled = NO
中完成我现在不知道,但我在 didReceiveRemoteNotification 中得到了 print(...),但没有得到弹出窗口。我从 Firebase -> Console -> Notification -> Single device 发送消息,并在此处复制我从 xCode Console -> func tokenRefreshNotificaiton 获得的令牌
在控制台中获取下一个,但不弹出
<FIRAnalytics/INFO> Firebase Analytics enabled
InstanceID token: TOKEN_ID
Connected to FCM.
===== didReceiveRemoteNotification ===== %@ [notification: {
body = test;
e = 1;
}, collapse_key: com.pf.app, from: 178653764278]
【问题讨论】:
-
我面临同样的问题。我正在运行来自 Quickstart 存储库的示例。任何帮助将不胜感激!
-
可能的解决方案:) stackoverflow.com/questions/37538330/…
-
@Svitlana 你有什么解决办法吗?
-
我遇到了同样的问题..试试这个..stackoverflow.com/questions/37929979/…
-
@V1P3R 非常感谢!看起来很有帮助会试试!
标签: ios firebase google-cloud-messaging firebase-cloud-messaging firebase-notifications