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.

4.5 (8) · 1 Review

Download materials
Save for later
Share

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
Note: While you can test local push notifications on a simulator, you need to be running on a physical device to receive push notifications from FCM.

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.

Screenshot of the app running, showing two good news items

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.

Screenshot showing the 'Keys' heading of the Certificates, Identifiers, & Profiles page, with the + button next to the heading circled in red

Give it a name and enable the Apple Push Notifications service (APNs) service. Select Continue and, on the next screen, select Register.

Creating a new p8 certificate with name

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.

Save your p8 and key ID

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.
Note: If you don’t need to track how your users interact with push notifications, you can disable Google Analytics and skip setting it up.

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:

Firebase Project Settings

Next, set up an iOS app under the General section of your project settings:

Select the iOS project setup

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.

Add p8 file to project

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.

Note: At the time of writing, the most recent version of Firebase was 7.3.

Next, add GoogleService-Info.plist to your project in the Assets group of your Xcode project:

Add GoogleService-Info plist to project

Finally, you can start adding code to your app!