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:
All chapters:
Realtime chat iOS demo (1) - Firebase Realtime Database
Realtime chat iOS demo (2) - Firebase Remote Config
Realtime chat iOS demo (3) - Firebase Cloud Storage
Realtime chat iOS demo (4) - Firebase Cloud Function
Realtime chat iOS demo (5) - Firebase Cloud Messaging
Source code:
TarotChatRoom in Github
No comments:
Post a Comment