Firebase Cloud Messaging for iOS: Push Notifications
Learn how to use Firebase Cloud Messaging to send and receive remote push notifications in your SwiftUI iOS app. By Andy Pereira.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Firebase Cloud Messaging for iOS: Push Notifications
25 mins
- Getting Started
- Configuring Firebase
- Creating the p8 Certificate
- Setting up the Firebase Project
- Adding the Package
- Configuring Your App
- Launching Firebase
- Registering for Notifications
- Sending Notifications
- Sending Data in Notifications
- Subscribing to Topics
- Sending Images
- Adding a Notification Service Extension
- Customizing Notifications
- Analytics
- Tracking Custom Events
- Where to Go From Here?
The ways people interact with apps vary from person to person. This can make it difficult to know when your customers will be using your app, especially if you need to give them important information. Push notifications provide a unified, consistent way to inform your app’s users of just about anything.
To receive a push notification, your device needs to be registered with the Apple Push Notification service (APNs) by receiving a unique device token. Once the device is registered, you can send push notifications to the device by sending a request to APNs using the device token. All this communication needs to happen from some sort of web server.
You can implement your own web service to communicate with APNs, but there are easier options. One of those is Firebase Cloud Messaging (FCM). With Firebase Cloud Messaging (FCM), you have an easy-to-use system at your fingertips! FCM handles the cloud aspect of push notifications, letting you send and receive pushes without having to write your own web service.
In this tutorial, you’ll learn how to:
- Set up your SwiftUI app to receive push notifications via Google’s Firebase Cloud Messaging using the Swift Package Manager
- Send notifications with media
- Receive notifications based on topics
- Add information to your app from the contents of a push notification
- Get information about how often push notifications are opened
To follow along with this tutorial, you’ll need:
- An paid Apple Developer Program membership to use push notifications with your app
- A Google Firebase account. You won’t need to pay anything to follow along with this tutorial. If you choose to use Firebase in a production environment, you can find pricing information on the associated site.
- A physical iOS or iPadOS device so you can receive push notifications
Getting Started
To get started, select the Download Materials button at the top or bottom of this tutorial.
The app you’ll use, Good News, gives users a news feed of only, well, good news. This is because even though there are bad things happening in the world, it’s important to remember there are good things happening as well!
In the starter folder, open GoodNews.xcodeproj. Open the Project navigator and select the Good News target. In the Signing & Capabilities tab, select your development team and enter a new Bundle Identifier. Build and run on either a real device or in a simulator, for now.
You’ll see the app has a feed for showing news articles to users, along with a tab where users can choose to subscribe to topics.
Configuring Firebase
Next, you’ll learn how to configure Firebase.
Creating the p8 Certificate
Firebase requires you to upload a p8 certificate to your app. This is a special file containing a private key that allows Firebase to send notifications. To get a p8 certificate, sign in to Apple Developer.
Select Certificates, Identifiers & Profiles and go to Keys. Select the circle + button to create a new key.
Give it a name and enable the Apple Push Notifications service (APNs) service. Select Continue and, on the next screen, select Register.
It’s important to note down the following three items from this screen:
- Select Download to save the p8 file locally. You’ll need to upload this to Firebase. You cannot download this after leaving this screen.
- Copy and save the Key ID to a file.
- Copy and save your Apple membership ID. This is next to your name in the upper-right corner of the Membership Center or under Membership Details.
Setting up the Firebase Project
Next, go to your Firebase account and select Go to console in the upper-right corner of the page. Select Add project and do the following to create your project:
- Use the name Good News.
- Enable Google Analytics.
- Choose a name and the country for Google Analytics.
- Use the default analytics settings.
Then, you’ll need to configure Firebase with your Apple p8 and membership information. Within your Firebase project, select the gear next to Project Overview and choose Project settings:
Next, set up an iOS app under the General section of your project settings:
From there, go to the app configuration page:
- Add the Bundle Identifier for your project.
- You can leave App nickname and App Store ID blank if you want.
After registering your app, download GoogleServices-Info.plist. You’ll need this to configure Firebase in your app later. You can select Next for the remaining steps. You won’t be using CocoaPods, and you can ignore the steps for adding initialization code to your app for now. :]
Next, upload your p8 certificate by going to Cloud Messaging in your Firebase project settings. Under APNs Authentication Key, select Upload.
You’ll see a pop-up asking you to:
- Upload the .p8 file you downloaded from Apple.
- Enter the Key ID you saved when creating the p8.
- Enter your Apple membership ID.
Select Upload to finish setting up your Firebase project.
Adding the Package
Now, you’ll use Swift Package Manager to add the Firebase dependency to your project. In Xcode, select File ▸ Swift Packages ▸ Add Package Dependency…. In the Choose Package Repository pop-up, enter https://github.com/firebase/firebase-ios-sdk.git.
Select Next, keeping the default options, until you get to a screen with a list of packages. This might take some time while Xcode downloads the necessary data. Select the following packages from the list:
- FirebaseAnalytics
- FirebaseMessaging
If you don’t want to collect information about how your users are using push notifications, feel free to leave FirebaseAnalytics unchecked. After adding these packages, it may take a few minutes to add and build the dependencies.
Next, add GoogleService-Info.plist to your project in the Assets group of your Xcode project:
Finally, you can start adding code to your app!