Ionic Capacitor Push Notifications with Firebase: A Step-by-Step Guide
Learn how to integrate push notifications in your Ionic Capacitor app using Firebase, with step-by-step instructions for both Android and iOS platforms.
In this tutorial, we will integrate push notifications in an Ionic Capacitor app using Firebase. You don’t need a specific service for this, but you do need to configure several things beforehand. Firebase is an excellent choice since it’s required for Android, and you can easily use it to send notifications without using the database.
First, we will create an Ionic app with Capacitor enabled and specify our package id, which is the unique identifier for your app. Then, we will build the app and add the native platforms.
If you already have an app, you can change the capacitor.config.json to include your appId. However, if your native folders already exist, you will need to replace the id in all files where it appears, as Capacitor only creates the folder once and won’t update the id itself. In the capacitor.config.json, you can also specify options like updating the badge count, playing sound on push, and showing an alert when a notification arrives.
Now, let’s configure push notifications outside the app.
If you have a new app, you should see “Get started by adding Firebase to your app” in your app’s dashboard. Otherwise, click the gear icon and go to project settings to add an app.
The dialog for both iOS and Android looks similar, and the important thing is to use your package id for the apps.
After the initial step, download the following files:
google-services.json file for Android
GoogleService-info.plist file for iOS
Next, configure the platforms.
Android Push Preparation
For Android, move the google-services.json file you downloaded to the android/app/ folder.
The Bundle ID should be the same as your App ID within Capacitor and Firebase.
Now, create a Key and enable the Apple Push Notifications service (APNs). If you have reached the maximum number of keys, you can use an existing key or a certificate instead, but the process is more complicated.
After downloading the .p8 file, upload it to Firebase. Open the Cloud Messaging tab in your Firebase project settings, upload the file, and enter the details for the Key ID and your Team ID from iOS.
Now, make changes to your Xcode project by running:
Copy the GoogleService-Info.plist file you downloaded from Firebase into your iOS project. Drag the file into the Xcode project inside the app/app folder, and select Copy items if needed.
Next, add a new Pod for the Firebase dependency in the ios/App/Podfile:
Update the native platform with this command:
Modify the native Swift code in ios/App/App/AppDelegate.swift to register with Firebase and return the correct token to your app.
Finally, add the Capability for Push Notifications within your Xcode project.
Now, build your app and integrate push notifications.
Ionic Push Notification Integration
Create a service and a new page in your Ionic project:
Update the routing in app/app-routing.module.ts to include the new page with a dynamic id:
Create a service to handle push notifications in services/fcm.service.ts:
Call the initPush() function in app/app.component.ts:
Handle the information on the details page in pages/details/details.page.ts:
Display the details in pages/details/details.page.html:
Build the app, sync your changes, and deploy it to your device.
Now, you can send push notifications with Firebase.
Sending Push Notifications with Firebase
There are several ways to send push notifications with Firebase.
Specific Device Test
After deploying your app to a device, you can check the console logs to see the token after registration. Use this token to send a targeted test push to confirm your integration is working. In Firebase, go to Cloud Messaging and select Send test message. Add the device token from the logs.
If everything is set up correctly, you should see a push notification on your device.
Push Message with Payload
To test a push notification with additional information, follow the wizard on the same page to specify general information and select the platform you want to target. Add additional options to send a payload with your push notification.
In the Advanced options section, add a Custom data key-value pair. For example, you can use the key detailsId and a value of your choice. This data will be used in the app to navigate to the details page with the specified id.
After sending the push notification, your app should receive it and display the details page with the specified id when the notification is tapped.
Using Firebase API
You can also send push notifications programmatically using the Firebase API. To do this, you need to obtain the Server key from your Firebase project settings under the Cloud Messaging tab.
With the server key, you can send a POST request to the Firebase API with the required payload. Here’s an example using Node.js and the request library:
Replace YOUR_SERVER_KEY and YOUR_DEVICE_TOKEN with your actual server key and device token. Run the script, and your device should receive the push notification with the custom payload.
That’s it! You’ve successfully integrated push notifications in your Ionic Capacitor app using Firebase. Now you can send push notifications to your users on both Android and iOS platforms.
Latest from news
capgo gives you the best insights you need to create a truly professional mobile app.