【发布时间】:2017-01-13 16:22:20
【问题描述】:
我无法让我的应用在后台或关机模式下接收通知。我已按照 Firebase 指南了解如何在我的应用中实现 firebase 消息传递。以前我使用过 GCM(谷歌云消息传递)并且一切正常,但是自从升级到 Firebase 后我无法让它工作。一旦我启动我的应用程序,我在后台或关闭时发送的所有通知(通过 firebase 控制台通知)都会传递。
我有:
- 在 Firebase 控制台上创建项目
- 使用正确的包 ID 将我的应用添加到 firebase 项目
- 创建了 APNS 开发证书
- 已将证书上传到我在 Firebase 控制台上的应用
值得一提的是,我通过在 Info.plist 文件中将 FirebaseAppDelegateProxyEnabled 设置为 NO 来禁用 swizzling。
相关代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
FIRApp.configure()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM")
} else {
print("Connected to FCM.")
self.refreshToken()
}
}
}
func refreshToken(){
if let refreshedToken = FIRInstanceID.instanceID().token() {
gcmToken = refreshedToken
userDefaults.setValue(gcmToken, forKey: CONSTANTS.GCM_TOKEN)
if(userDefaults.boolForKey("UserLoggedIn")){
pushGcmToken() //push the token to server
}
}
}
func onTokenRefresh() {
refreshToken()
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
NSLog("didReceiveRemoteNotification \(userInfo)")
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
handleRemoteNotification(userInfo)
}
func handleRemoteNotification(userInfo: [NSObject : AnyObject]){
if let notification = userInfo["notification"] as? [NSObject : AnyObject]{
let bodyNot = notification["body"] as! String
var titleNot = "Ändring"
var category = "UNIFIED_OTHER_CATEGORY"
if(notification["title"] != nil){
titleNot = (notification["title"] as! String == "Call" ? "Inkomande samtal" : notification["title"]) as! String
category = "UNIFIED_CALL_CATEGORY"
}
let notis = UILocalNotification()
notis.alertTitle = titleNot
notis.alertBody = bodyNot
notis.soundName = UILocalNotificationDefaultSoundName // play default sound
notis.userInfo = ["UUID": "122" ] // assign a unique identifier to the notification so that we can retrieve it later
notis.category = category
notis.fireDate = NSDate()
UIApplication.sharedApplication().scheduleLocalNotification(notis)
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
NSLog("didRegisterForRemoteNotificationsWithDeviceToken \(deviceToken)")
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
我什至尝试过 swizzling。同样的事情正在发生。我将非常感谢任何帮助或提示正确方向。
【问题讨论】:
-
嗨,你找到答案了吗??
-
你有任何相当于
content-available = 1的firebase吗?
标签: ios swift firebase apple-push-notifications firebase-cloud-messaging