Monday 25 February 2019

Realtime chat iOS demo (5) - Firebase Cloud Messaging

This chapter is the second part of the previous one to setup the Firebase Cloud Messaging and requires a developer account. In the end, when the host signs in/out in our realtime chat demo app, a notification should be delivered to the guest on real devices.

Preparation and Setup:

  • Go to Apple's developer console, register a bundleID  with Push Notification turned on
  • Apple's developer console -> App IDs -> bundleID -> scroll down and click Edit -> scroll down to the Push Notifications section -> Create Dev SSL Certificate -> continue till a step requires a CSR file
  • Open Keychain Access on mac -> Keychain Access menu option -> Certificate Assistant -> Request a certificate from... -> choose Save to disk and fill out with your email and name  of Firebase account
  • Go back to Apple's developer console, upload the CSR file created in the previous step and download the aps_development.cer
  • Double click aps_development.cer to open it in Keychain Access -> Export the private key as .p12 -> Remember the password protect .p12 file
  • Go to Firebase console -> Project OverView -> Project Settings -> Cloud Messaging tab -> upload .p12 created in previous step with the password

App side preparation and setup:

  • Install SDK: Firebase/Core, Firebase/Messaging
  • Turn on Push Notification in target capabilities tab
  • Make sure GoogleService-info.plist exists(added in previous chapters)


AppDelegate.swift modification:

  • Import modules 


  • Make AppDelegate adopt UNUserNotificationCenterDelegate, MessagingDelegate
  • Add the following in didFinishLaunchingWithOptions


Note: 

  • InstanceID is the FCM registration token, which is required when you want to send notifications to a particular group via admin.messaging().sendToDevice(token, payload) in cloud functions. FCM registration token is also required when you testing cloud messaging on a particular device. For simplicity, I will just use this hardcoded token in cloud function to send push notification. 
  • When using Firebase Cloud Messaging we don't have to handling the remote device token directly in method didRegisterForRemoteNotificationsWithDeviceToken and it is different from the FCM registration token. didRegisterForRemoteNotificationsWithDeviceToken  is actually swizzled by Firebase.

Testing via Cloud Messaging:

Now go to Cloud Messaging in Firebase console to manually send a notification. If everything is setup correctly,  you device should receive a notification.

Final Step:

Just change the console.log() inside the cloud function to the real logic of sending notification and deploy it again.

If you are facing any syntax error about 'async',  try the steps blow:

  • npm install firebase-functions@latest
  • add  "engines": { "node": "8" } in functions/package.json
  • deploy again


Sign in as Sylvia on simulator, a notification should arrive on the real device:

No comments:

Post a Comment