Push Notifications in Firebase

      4 Comments on Push Notifications in Firebase

If you have an iOS app and you wanted to have push notifications, Firebase offers a simple way to integrate push notifications into your app. And best of all, it’s free. In this post, we’ll cover setting up your certificates, keys, app and Firebase for push notifications.

Create a Key

You will need to upload a .p8 file to Firebase in order to use push notifications. There are good tutorials in Firebase documentation, but this step was a bit misleading, so I’ll cover it here first.

Despite what the documentation says, you’ll need to create a key, so log into your Apple Developer account, and select ‘Certificates, Identifiers and Profiles’:

In the menu to the left, you’ll see a section ‘Keys’, select ‘All’:

In the top right corner you will see a plus sign, click on it to create a new key, don’t forget to enable APNs:

Continue to the next step, where you can review the information you inputted:

Once you review the information you provided and you’re happy with it, proceed to the last step:

Your key is created. Download it and proceed. Make sure you backup the key, because there’s no way to get to this key again after this step. It’s not stored on the Apple’s servers. And write down the Key ID and key name, you’ll need it later. Now if you select all keys, you should see your key there:

When you download the key, you’ll see it’s the .p8 file you need, we’ll use this later to setup Firebase. Now let’s quickly enable push notifications for your app.

Create App ID

You probably know how to create an App ID, but let’s cover it anyway. In the ‘Identifiers’ section select ‘App IDs’:

In the upper right corner select the plus button to create a new App ID, give it a good name, select the team ID and enter the bundle ID, remember the team ID, you’ll need this later:

Then in the ‘App Services’ section select push notifications, and continue:

That’s pretty much it, you have an App ID. You’ll need a provisioning profile for your app, you’ll need a development and distribution profile. The process of creating both is pretty much the same, so let’s cover creating the development profile here.

Create a Development Provisioning Profile

You’ll find the ‘Provisioning Profiles’ section at the bottom of the menu, select ‘All’ from the menu:

Select the ‘iOS App Development’, development certificate, app id, and devices, then select continue:

That was a lot of screenshots and very little text… Almost like National Geographic. The boring part is done. Go to Xcode preferences, select ‘Accounts’, find your account and download all profiles to get this new profile you created. Let’s set the project up.

Project Setup

There’s actually not much you need to do to enable push notifications in your project, simply go to the ‘Capabilities tab’ and enable push notifications:

When these two steps complete, you’ll see a new file created in the project explorer with the extension .entitlements (the name of the file will be the same as the target name). With this step and the .p8 file you created earlier, you can receive push notifications. Now you simply need to set up Firebase.

I’m using Cocoapods, so in my Podfile, I added a new pod:

When you run pod install, you’ll get the libraries you need. Now in the AppDelegate you’ll have to register to receive push notifications, so let’s do that:

Call this method from your ‘applicationDidFinishLaunching’ method.

That’s it. For those of you who used push notifications before, Firebase is swizzling push notification callbacks, so you don’t have to handle notification settings registration and token registration/failure callbacks.

With this you will be able to receive push notifications, if you want to perform some action when you receive it, you’ll have to implement two more delegate callbacks. ‘willPresent’ callback will get called if the app is in the foreground and the user receives the notification, ‘didReceive’ will be called if the app is in the background. For example, I’m sending a custom payload that will refresh my data source when a notification contains that payload. Let’s see this on an example:

Every time I publish a new post I’ll send a push notification with a custom payload. The payload is simply a dictionary that you can easily process and perform custom actions, like refresh your posts in the app, for example. Build and run the app on your device and allow the app to receive push notifications.

Now we have the app set up and listening for push notifications, let’s set up Firebase.

Firebase Setup

It’s time to use that .p8 key you created. Open your Firebase project, select the little cog-wheel next to the ‘Overview’ and select ‘Project Settings’:

In your project settings select ‘Cloud Messaging’, you’ll see a screen like this:

Now simply upload your .p8 file here. When the upload is finished, you’ll need your key name, key ID and team ID. If you didn’t note these down as you were following this tutorial you can always go to Apple Developer and find this info. You should be all set, but let’s test if things work anyway.

Sending Your First Push

In your Firebase console select Notifications:

And then select ‘New Message’ at the top, you’ll see a screen like this one:

Type in your message text and select your app from the drop down menu. If you just want to send a simple push, this is all you need to do. You can schedule a push notification by selecting a delivery date. In my case I wanted to deliver a custom payload with the push notification, so let’s quickly see how to do that. Expand the advanced section:

In the ‘Custom data’ section add your keys and values and they will be delivered with your push notification. Once you’re done with configuration press send, confirm your selection and you should see the message appear within a couple of seconds. In Firebase you can see your messaging campaigns:

And in the app you’ll see your push notification:

It works, how cool is that 🙂

Conclusion

If you need to send simple push notifications to your users and you don’t have your own server, or don’t want to pay a third-party provider Firebase might definitely be worth considering. Even if you don’t use other features of Firebase, you can only use push notifications, they are completely free. It’s relatively easy to set up and it just works 🙂

This post had a lot of screenshots, sorry about that 🙂 I hope you managed to set your Firebase up for push notifications and that you found something useful here 🙂

Have a nice day 🙂

Dejan.

More resources

0

4 thoughts on “Push Notifications in Firebase

  1. Swapnil S

    Was there any specific reaaon for using .p8 key generation process, cos in my experience, i never had to that(i implemented an ios firebase push solution two months back)

    Reply
    1. Dejan Agostini Post author

      Hi Swapnil,
      It was the only file I could upload, I couldn’t upload the .p12. Maybe I missed something, maybe they changed it…

      Reply
  2. Will Boland

    It didn’t work. I followed this tutorial word for word and still hasn’t worked. Same with all other ones I’ve tried on Push Notifications.

    Reply
    1. Dejan Agostini Post author

      You mean you tried other tutorials on push notifications and they all failed? Did you enable push notifications on your provisioning profile? That’s my first suspicion.

      Reply

Leave a Reply