Integrating the Push Notifications in Android APP
Last updated
Last updated
# Integrating the Push Notifications in Android APP
This guide provides step-by-step instructions on how to create an Android app using the PortSIP VoIP SDK to receive VoIP push notifications sent from PortSIP PBX.
The official documentation can be found here. Some of the advantages include:
The app is automatically relaunched if it’s not running when a VoIP push is received.
The device is woken up only when a VoIP push occurs, saving battery life.
VoIP pushes go straight to your app for processing and are delivered without delay.
Since PortSIP PBX uses Google Firebase to send push notifications, we need to configure some settings to get this working.
Add a new project to the Firebase console.
Set the project name and country. For example, you can name your project SIPSample
.
Select the SIPSample Project
Select “Project settings”
Select “Android platform”
Set a package name for your app. I only set my package name and omit the SHA-1 because I do not use Firebase for my app's authentication.
Click the REGISTER APP button to download google-services.json
. This is an important file, and you will need to place it into your app.
Please note that the Android package name is also referred to as the App ID. We will use it in future settings.
google-services.json
to Your App FolderDownload the PortSIP VoIP SDK Sample Project: Obtain the sample project and open the PUSH SIPSample
project using Android Studio.
Replace the google-services.json
File: Navigate to the SIPSample
folder and replace the existing google-services.json
file with the one you just downloaded.
Load the Google Services Plugin: The Google services plugin for Gradle will automatically load the new google-services.json
file.
Open Android Studio: Launch Android Studio and navigate to your project.
Modify the build.gradle
Files: Update your Gradle files to integrate the Google services plugin.
Update the Project-Level build.gradle
: Open the build.gradle
file located in your project folder and add the following line:
Update the App-Level build.gradle
: Open the build.gradle
file located in your project/SIPSample
folder and add the below line at the end of the file.
Add Firebase Dependencies: Include the necessary Firebase dependencies under the dependencies
section in the same build.gradle
file.
Update Google Play Services: If your project uses any functions from com.google.android.gms:play-services
(e.g., GPS location), you need to update their versions as well. As of writing this tutorial, version 20.0.0
works well. If you encounter compilation problems, check for the correct version number.
compile 'com.google.android.gms:play-services-auth:20.0.0' &#xNAN;compile 'com.google.android.gms:play-services-identity:20.0.0'
Add applicationId
to defaultConfig
Section: In the defaultConfig
section of your build.gradle
file, add the applicationId
:
To use Firebase Cloud Messaging (FCM) in your app, you need to add two services: one for testing if push notifications work, and another for receiving or sending messages according to your app’s design.
Add a Service that Extends FirebaseMessagingService
: To receive notifications in your app, create a service that extends FirebaseMessagingService
. Here’s an example:
Add the Service to AndroidManifest.xml
: To ensure your service is recognized by the system, add it to your AndroidManifest.xml
file:
To verify that your setup works, follow these steps to send a test message to your mobile device:
Install and run the app on the target device.
Make sure the app is in the background on the device.
In the Firebase console, open the Messaging page.
If this is your first message, select Create your first campaign.
a. Select Firebase Notification messages and select Create.
Otherwise, on the Campaigns tab, select New campaign and then Notifications.
Enter the message text. All other fields are optional.
Select Send test message from the right pane.
In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.
Select Test.
After you select Test, you should receive a push notification on your Android mobile. If your app is running in the background, you will see it in the mobile’s notification center. Otherwise, you can see it in your Android Monitor log (ensure you have code to log incoming messages) like this:
If the setup is successful, you should get a notification on your mobile. Sometimes, it may take a couple of minutes for the message to be sent and received, so just be patient and wait for a little while.
X-Push
to REGISTER MessageTo inform PortSIP PBX that this client has enabled push notifications, you need to add the SIP header X-Push
to the REGISTER message. Here’s an example of how to do this:
Compilation Issues: Compilation problems can often be related to incorrect version numbers in your build.gradle
files. Ensure that all dependencies are using compatible versions.
Firebase Crash Analytics Warning: If you see a message like "com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization." in your Android Monitor log, it is okay. This message appears because we are not using the Firebase Crash Analytics service.
Access Project Settings: In the Firebase console, click the Settings button and choose the Project Settings -> Cloud Messaging -> Mange Service Accounts.
Srvice Accounts: click the Manage keys.
Create new key
Choose Key type “JSON”, and click “CREATE”, you will get a JSON file, e.g: sipsample-c3789-c6a094e0e0e2.json
Sign In: Log in to the PortSIP PBX Web Portal.
Navigate to Mobile PUSH Settings: Select the menu Settings > Mobile push notifications.
Add a New App: Click the Add New App button. You will see the following screen:
Please set the following items:
Enabled: Check this option to enable push notifications. Uncheck it to disable PUSH notifications.
Server Type: Apple and Google both provide a production PUSH server and a development PUSH server for sending PUSH notifications. The development server is usually used during the development stage. Once your app is released, you can change this setting to the production server.
App ID: Enter the App ID that you created in step 3. Note: This ID is case-sensitive.
Google Server Key and Google Sender ID: Enter the key and ID that you noted in step 10.2.
Click the Apply button to enable the push service in PortSIP PBX.