Reputation: 177
I have problems getting my app to receive notifications while in background or shutdown mode. I have followed Firebase guide on how to implement firebase messaging in my app. Previously I have used GCM (google cloud messaging) and it all worked well, but since upgrading to Firebase I can't get it to work. As soon as I start my app, all the notifications that I sent (through firebase console notifications) while in background or shutdown are delivered.
I have:
Worth mentioning is that I have disabled swizzling by setting FirebaseAppDelegateProxyEnabled to NO in my Info.plist file.
Relevant code:
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)
}
I have even tried with swizzling on. Same thing happening. I would very much appreciate any help or hint into the right direction.
Upvotes: 4
Views: 1992
Reputation: 439
You have to add "content-available" : true in the JSON payload. Otherwise you won't get push notification in the background mode.
"notification" : {
"content-available" : true,
"body" : "this is body",
"title" : "this is title"
}
Upvotes: -1
Reputation: 873
I configured below things and its work fine for my application,
In info.plist set below two keys
<key>FIRMessagingAutoRegisterEnabledflag</key>
<true/>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
In appdelegate set below code for test notification development mode
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
Download GoogleService-Info.plist from firebase console and put it on your application
Upvotes: 1