Firebase notifications when app in foreground vs background

1,430 views
Skip to first unread message

ro...@aheadofthecurve.co.za

unread,
Jul 19, 2016, 10:07:13 AM7/19/16
to Firebase Google Group
Hi,

I'm attempting to implement iOS Firebase Notifications.

In short, I receive notification when the app is in the foreground just fine, but I'm not getting remote notifications when the app is closed or in the background.

My AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

   
var window: UIWindow?
 
    func application
(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
         
        let settings
: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application
.registerUserNotificationSettings(settings)
        application
.registerForRemoteNotifications()

       
FIRApp.configure()

       
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)

       
print("... \(FIRInstanceID.instanceID().token()!)")

       
return true
   
}

     

    func application
(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],

                     fetchCompletionHandler completionHandler
: (UIBackgroundFetchResult) -> Void)

   
{

       
print("%@", userInfo)

   
}


 

    func tokenRefreshNotification
(notification: NSNotification)
{

        let refreshedToken
= FIRInstanceID.instanceID().token()!

       
print("InstanceID token: \(refreshedToken)")

        connectToFcm
()
   
}


 

    func connectToFcm
() {

       
FIRMessaging.messaging().connectWithCompletion { (error) in

           
if (error != nil) {

               
print("Unable to connect with FCM. \(error)")

           
} else {

               
print("Connected to FCM.")

           
}

       
}

   
}

     

    func applicationDidBecomeActive
(application: UIApplication)

   
{

        connectToFcm
()

   
}


 

    func applicationDidEnterBackground
(application: UIApplication)

   
{

       
FIRMessaging.messaging().disconnect()

   
}

}


Kato Richardson

unread,
Jul 19, 2016, 11:16:38 AM7/19/16
to Firebase Google Group
Hi Romeo,

Looks like this discussion topic has been hammered pretty thoroughly already.



One common issue here seems to be a misunderstanding between FCM and Notifications sent via the console (not the same thing). It seems like you can't use onMessageReceived() to capture Notifications in the same manner as items sent via FCM.

Also pay special attention in the docs to the difference in behavior when a data payload is included vs not, as well as the section on "Handle messages in a backgrounded app", which covers some special configurations needed for this to work (i.e. click_action and intents).

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/a3b95ccd-b7e4-476f-83a7-beb9bb5acfcf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages