by Filip Jerga
How to get push notifications working with Ionic 4 and Firebase
A full step-by-step tutorial that will get you on the right track for iOS and Android
Setting up push notifications can be truly frustrating and time consuming. So I went through all of the setups and prepared this tutorial for you.
Ionic 4 should already be installed.
- Package installation
- Firebase setup for Android and iOS
- Push notification code implementation
- Test push notifications on Android
- iOS push notification pre-setup
- Test push notifications on iOS
1. Package installation
Open your Ionic project in the coding editor of your choice, and open your terminal as well. Navigate to your project’s folder.
First, we will install all of the required packages.
What we need to install:
- Cordova plugin for firebase:
ionic cordova plugin add cordova-plugin-firebase
- Firebase native package: since Ionic 4 is in beta, check your Ionic-native packages in
package.jsonand install the same version as other Ionic-native packages. Finally, let’s type:
npm install --save @email@example.com
- One last package, AngularFire 2. This is a library for Angular and Firebase:
npm install --save angularfire2 firebase
Packages install, done! Let’s move to the second section.
2. Firebase setup for iOS and Android
Before we will start all of the setup, I need to warn you that you cannot test your push notifications on iOS emulator. To test it out you need to have an Apple Developer account, which costs around $99USD per year. I suggest you to go through iOS setup anyway so it will give you a better understanding for future projects.
Note: The steps starting here are very important, so please be patient. Read slowly and make sure you get everything right. Looking for issues after all of the setup can be very frustrating, trust me — I am speaking from my own experience.
Navigate to the Firebase page and login to a console. If you don’t have a project created yet, do it now. You should see this screen.
Click on the iOS button and you’ll see this:
Now we need to provide our iOS bundle ID and this has to be the same as in your Ionic project. Let’s say I want to have the bundle name
com.filipjerga.angularcourse, then I need to do the following:
Open your Ionic projects and navigate to “config.xml” file. Let’s inspect the widget element. The Id attribute holds the unique identifier of your application. I said before if you specified your bundle name to
com.filipjerga.angularcourse in Firebase, the id in your Ionic project has to be the same! You can also leave the id as you have it already in your Ionic project but then you need to change it in Firebase.
After you get the value of id, don’t forget to provide it to your Firebase application as the bundle ID.
That should be everything in the first step of registering the application. This step is crucial, so double check the value of id on the widget and bundle ID of your Firebase application.
Leave the other fields blank and click “Register app”.
Now we need to download “GoogleService-Info.plist”.
When it’s downloaded, paste it into a base folder of your projects. You can see a folder structure in my project here.
We can skip all of the further steps, as they are not required for the Ionic project setup. You should have your IOS application ready.
The following steps for Android are almost the same as for the iOS setup:
- Click ‘add app’ for Android, same as we did in iOS before.
- The Android package name needs to be the same as our widget id, in my case:
- Next, download
google-services.json. Same as before with the iOS file, we need to copy it to the base folder of our applications
- Click “Next” until you are on the last step, which you can skip, and you should end up with both applications created.
Yay! Congrats! But it’s still too early to cheer.
3. Push notification code implementation
The time has come to finally warm up our fingers by typing some code. We will start with importing the packages we installed before.
- Go to
- Your file should look like this:
You can see
Firebase in the providers array, and
AngularFireModule in imports.
But where did the
config object came from ? You can see lot of information there as “apiKey, authDomain” and so on.
To answer this we need go back to our Firebase console and create a web app.
You need to click on a web platform icon on the right from the Android icon (see the image above). When the web app is selected you will be presented with your own config object.
Now it’s time to copy whole config object to
app.module.ts in our Ionic projects. Please make sure you change it for your config object! Mine will not work for you.
Now we can start working on the implementation of the push notification service.
Push notification service
Let’s create a new service. Call it what you like. I will call mine
fcm.service.ts. FCM stands for Firebase Cloud Messaging.
First, let’s take a look at the service implementation. I will explain it line by line.
If we want to send a push notification to a device, we need to get an unique identifier of the device. In this case it’s called a
We need to check for a platform specific device, because of an additional step in the iOS setup. iOS requires explicit permissions to receive push notifications.
Now we need to store this token somewhere, but where? We will store tokens in the Firebase database. You can see, I am creating device collections and I am filling them with
data that contains the
token and just a testing
UserId. Perfect! Now, we have stored our token and we can subscribe to notifications.
Subscribing to notifications is actually very simple. We just need to call
Amazing. Service checked!
All of this is nice but kinda useless, since we are not using our service yet. Let’s fix it!
Move to your
app.component.ts and write the following:
Especially important here is the function
We are getting an unique token of the device first.
We are also subscribing to incoming notifications from Firebase.
When the message is received, we need to check, again, for specific platforms. On iOS your message text is under
aps.alert . On Android it’s under
Then we will just simply display the received message as a
Now the code setup is done. We are getting very close! It’s time to test it out.
4. Test push notifications on Android
All of the required setup for Android should be done right now. You can start emulating your app by:
ionic cordova emulate android
ionic cordova build android and open your build manually in Android Studio.
Let’s launch our applications and go to a Home menu, so we will see a push notification there. Make sure your application was launched properly in emulator and you have no errors.
Go back to your browsers to your Firebase Applications. Now it’s time to inspect our Firebase Database. You can find the database option in the left panel under the Develop category.
After your application was launched in emulators, the code from app.component.ts we wrote a few moments ago ran. No wonder our database is populated. In the “saveToken” function we specified the “devices” collection and we saved the token with the user id as a document into this devices collection. That is what we see in our database.
In my case, I have multiple tokens in my database but you should have only one since we’ve run our app the first time. You will create a new document per unique device/emulator you are running your app on.
Now it’s time to copy this token in order to send push notification to a specific device.
Navigate to the left panel to a grow tab and click on cloud messaging.
Now we need to fill in the necessary data. Enter the text of your message and provide the token of the device from the database we just copied.
When you are sending a message, open your emulated device simultaneously and watch what happens.
Send a message, aaaaand Congrats! Now your Android setup is done and you are able to send push notifications! Isn’t that awesome?
5. iOS push notification pre-setup
Buckle up guys, iOs setup is coming. Let’s separate this setup into multiple steps, so we will not have a panic attack. Let’s dig in!
First, build your application for iOS:
ionic cordova build ios
Open your Xcode, and find your built app in
platforms/ios/nameofyourapp.xcodeproj . Open it.
This should open a tree structure of your application on the left side. Double Click the root file of this structure. This will open an additional menu with more settings for your app.
Sign in with your developer account.
5. Open the top “Capabilities” tab and enable “Push Notifications”.
6. Navigate to your Apple developer account page. Under “Certificates” select “All” and click "
+" to add a new certificate.
Enable the Apple Push Notification service and proceed to the next step.
Now let’s choose your application.
We need to request a certificate. On your Mac, go to “Keychain Access” -> “Certificate Assistant” -> “Request a Certificate From a Certificate Authority”.
Complete all the necessary information — your email and common name — and save it to disk.
In the Apple Console proceed to the next step and upload your certificate request.
In the next step your certificate should be created and you can download it. You will need it later.
Now we need to create a service key to enable Apple push notifications. Under “Keys” select “All” . Choose your key name. Enable “Apple Push Notifications service (APNs)”.
Click “Continue” and confirm your key. Never share such data with others. You can now download your key.
Now we need to go back to Firebase.
In Firebase, open your iOS application and navigate to “Cloud Messaging”.
We need to upload our APN Authentication Key we generated a while ago. Click “Upload”.
Provide all the information and upload the key.
First, upload your “.p8” file, downloaded from the Apple console earlier. Enter your Key ID. You can get the App ID prefix from the Apple console in “Identifiers”->“App IDs”->“Your App”->“Prefix”.
That’s it. Tears of joy are running down my face.
We can test Push Notifications on iOS. Let’s not forget we need to use a real device.
6. Test push notification on iOS
First, we need to build our applications so let’s run:
ionic cordova build ios
In Xcode you can run your application on a device connected by USB with your computer.
Let’s wait until everything is launched. We can now repeat the steps to send push notifications from Section 4, because it is the same as on Android.
Remember that you need to use a new token now, that was generated for your iOS device. Go to Databases, get a new token, and send a push notification. Your result should look like this.
I hope you have been successful with setting up your push notifications. It takes some time and patience to get everything right, but the outcome and benefits are amazing.
If you like my tutorial and you are interested in more, you can check out my course on Udemy: Ionic 4 Crash Course with Heartstone API and Angular.
For a full project see my Github Repo.