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.
{% hint style="warning" %} Please note that the Android package name is also referred to as the App ID. We will use it in future settings. {% endhint %}
- Download 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 theSIPSample
folder and replace the existinggoogle-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 thebuild.gradle
file located in your project folder and add the following line:
- Update the Project-Level
buildscript {
dependencies {
classpath 'com.google.gms:google-services:3.1.0' // Add this line
}
}
- Update the App-Level
build.gradle
: Open thebuild.gradle
file located in yourproject/SIPSample
folder and add the below line at the end of the file.
apply plugin: 'com.google.gms.google-services'
- Add Firebase Dependencies: Include the necessary Firebase dependencies under the
dependencies
section in the samebuild.gradle
file.
dependencies {
// this line must be included to integrate with Firebase
compile 'com.google.firebase:firebase-core:20.0.0'
// this line must be included to use FCM
compile 'com.google.firebase:firebase-messaging:20.0.0'
}
- 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, version20.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
todefaultConfig
Section: In thedefaultConfig
section of yourbuild.gradle
file, add theapplicationId
:
android {
defaultConfig {
applicationId "com.portsip.sipsamplepush" // this is the id that your app has
} }
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 extendsFirebaseMessagingService
. Here’s an example:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Map<String, String> data = remoteMessage.getData();
String type=data.get("msg_type")//”audio” ”video” ”im”
String content = data.get("msg_content");
String from = data.get("send_from");
String to = data.get("send_to");
String xpushid = data.get("x-push-id");
//new version (Portsip pbx>12.0)
String mimeType = data.get("mime_type");
}
@Override
public void onNewToken(String s) {
sendRegistrationToServer(s);
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(this,MyService.class);
intent.setAction(MyService.ACTION_TOKENREFRESH );
intent.putExtra(MyService.DEVICE_TOKEN,token);
startService(intent);
}
}
- Add the Service to
AndroidManifest.xml
: To ensure your service is recognized by the system, add it to yourAndroidManifest.xml
file:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
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:
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
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.
To 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:
import android.app.Service;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.portsip.PortSipSdk;
import androidx.annotation.NonNull;
public class MyService extends Service implements OnPortSIPEvent
{
String pushToken = null ;
public static final String ACTION_TOKENREFRESH = "token_refresh";
public static final String DEVICE_TOKEN = "deviceToken";
public PortSipSdk mSipSdk;
String appid=”com.portsip.sipsamplepush”;
//you app id
@Override
public void onCreate() {
...
//get device token
try {
//
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
pushToken =task.getResult().getToken();
}
}
);
}
catch (IllegalStateException e){
Log.d("",e.toString());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && ACTION_TOKENREFRESH.equals(intent.getAction())) {
pushToken = intent.getStringExtra(DEVICE_TOKEN);
Boolean supportPush = true;
setPushHeader(supportPush);
}
return START_REDELIVER_INTENT;
}
/**
* @param support true enable Push, false disable Push.
*/
private void setPushHeader(Boolean support){
if(TextUtils.isEmpty(pushToken )){
String pushMessage = "device-os=android;device-uid=" + pushToken +";allow-call-push=”+support+”;allow-message-push=”+support+”;app-id=”+appid;
mSipSdk.addSipMessageHeader(-1,"REGISTER",1,"X-Push",pushMessage);
if(sdk is online){
mSipSdk.refreshRegistration(0);
}
}
}
}
- 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.
- Firebase initialization is not starting
- 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.