Implement PUSH notifications in Android APP with PortSIP PBX 12.x
Last updated
Last updated
This guide gives you step-by-step instructions on how to create an Android App based on 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 VoIP push occurs (to save battery)
VoIP pushes go straight to your app for processing and are delivered without delay
The App is automatically relaunched if it’s not running when a VoIP push is received
Since PortSIP PBX uses the Google Firebase to send PUSH notifications, we need to configure some settings to get this working.
Add a new project to Firebase console. You will need to set the project name and country. For example, I will call my project SIPSamplePush.
Select "Add Firebase to your Android app". Set a package name for your app. I only set my package name and omit the SHA-1 because I don't use Firebase for my app's authentication.
Click the REGISTER APP button here to download google-services.json. This is an important file and you will need to put it into your app.
Important: Please note that the Android package name is also referred to as “App ID”. We will use it in future settings.
Now please download the PortSIP VoIP SDK Sample project and open it (PUSH SIPSample project) with Android Studio. Replace google-services.json in SIPSample folder. The Google services plugin for Gradle will load the google-services.json file you just downloaded.
Open Android Studio and modify your build.gradle files to use the Google services plugin.
(1) Update the project-level build.gradle (the one in your project folder) Add the following line to the build.gradle file:
(2) Update the app-level build.gradle (the one in your project/SIPSample)
a. Add below line to the bottom of the build.gradle file:
And Firebase related dependencies under dependencies in the same build.gradle file.
If you add Firebase into an existing project that uses any function of gms:play-services, such as GPS location, you have to update their versions as well. Upon writing this tutorial, 20.0.0 works well. If you get compilation problems, you need to check to find out 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'
Two services should be added to use Firebase Cloud Messaging service: a basic code for testing if push notification works, and other codes for receiving messages or sending messages in your app according to your design.
Add a service that extends FirebaseMessagingService
To receive any notification in your app, you should add a service that extends FirebaseMessagingService as below:
Add it into the AndroidManifest.xml file.
7. Test and send your first push notification
To see if the setup works, run a test by sending a test message to your own mobile.
In the Firebase console, write down your message and choose an app. Click "SEND MESSAGE".
Now you should get a PUSH notification on your Android mobile. If your app is running in the background, you will get it on the mobile's notification center; otherwise, you can see it in your Android Monitor log (we have to put a 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.
Adding SIP header "x-p-push” to REGISTER message. We use it to inform PortSIP PBX that this client has enabled PUSH.
Compilation problems can be related to wrong version numbers in your build.gradle files.
If you see a message like "com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization." in your Android Monitor log, it is OK as we don't use this Firebase Crash Analytics service.
(1) In the Firebase console, click the “Settings” button and choose “Project settings” menu.
(2). In the “Settings” tab, click “Cloud Messaging” tab. You will see the “Server key” and “Sender ID”, please note it down.
Now sign in PortSIP PBX 12.0 Management Console, select menu “Settings” > “Mobile PUSH”.
Click “Add New App” button, you will see below screen:
Please set the following items:
Enabled – check it to enable PUSH and un-check to disable PUSH.
Apple and Google both are provided with the production PUSH server and development PUSH server for sending PUSH notifications. The development production server is usually used for your development stage. Once your app is released, you can change this setting to the production server.
App ID – the ID that you created in step 3. Note: This ID is case-sensitive.
Google Server Key and Google sender ID – the key and ID are the ones you noted in step 10.2.
Click the “Apply” button and the PUSH service is enabled in PBX.