Integrating the Push Notifications in Android APP
Last updated
Last updated
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 SIPSamplePush
.
Select Add Firebase to your Android app.
Set a package name for your app. You can omit the SHA-1 if you don’t use Firebase for your 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' 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:
Run a Test: Open the Firebase console.
Compose Your Message: Write down your message in the console.
Select Your App: Choose the app you want to send the message to.
Send the Message: Click SEND MESSAGE to dispatch the notification.
Now, 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 menu.
Navigate to Cloud Messaging: In the Settings tab, click the Cloud Messaging tab. You will see the Server key and Sender ID. Please note them down.
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.