Add Notification Support to your Android App
This task builds on Android existing documentation.
Before you start
Familiarize yourself with Android Firebase Cloud Messaging service and Android notification documentation before undertaking any development. See https://developer.android.com/studio/write/firebase.html.onNewToken
and onMessageReceived
call-back functions, passing both of them onto the widget, as shown in the following
example. public class CommunicationFragment {
/**
* If it's the in-app call ringing.
*/
public static final String IS_IN_APP_CALL_RINGING = "isInAppCallRinging";
...
/**
* Set client information.
*
* @param clientInfo the client information
* @param context the android application context
*/
public static void setClientInfo(ClientInfo clientInfo, Context context);
/**
* Set in-app call content.
*
* @param notificationData the notification data
* @param context the android application context
* @param cls the communication fragment parent activity class
*/
public static void setNotificationContent(Map<String, String> notificationData, Context context, Class<?> cls);
...
}
public class ClientInfo {
String tenantKey;
String phoneNumber;
String applicationId;
String deviceToken;
public ClientInfo(String tenantKey, String userName, String applicationId, String deviceToken);
}
Update the widget to expose APIs for
onNewToken
and
onMessageReceived
and update your mobile app to register the
mobile device with the Firebase Cloud Messaging service to receive notification
messages. The mobile app needs to obtain a device token, then call the Live
Experience widget API to pass the token onto Live Experience, as shown in the
following example.
public class NotificationFirebaseService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData() != null) {
CommunicationFragment.settings.forceVideoInFullScreen = false;
CommunicationFragment.settings.startVideoInFullScreen = false;
CommunicationFragment.settings.noEndUserCallControls = false;
CommunicationFragment.settings.suppressAssociateInformation = false;
CommunicationFragment.settings.omitPreview = false;
CommunicationFragment.setNotificationContent(remoteMessage.getData(), this.getApplicationContext(), HomeActivity.class);
}
}
...
}
public final class MceDemoApplication extends MultiDexApplication {
public void setClientInfo() {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.i(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.d(TAG, "Refreshed token: " + token);
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MceDemoApplication.this.getApplicationContext());
final String serverAddress = sharedPreferences.getString(MceDemoApplication.SERVER_URL, "");
final String tenant = sharedPreferences.getString(MceDemoApplication.TENANT, "");
final String email = sharedPreferences.getString(MceDemoApplication.EMAIL, "john.smith@example.com");
final String fullName = sharedPreferences.getString(FULL_NAME, "John Smith");
final String phoneNumber = sharedPreferences.getString(PHONE_NUMBER, "+1-202-555-0171");
String appId = sharedPreferences.getString(MceDemoApplication.APPLICATION_ID, "");
appId = SecurityHelper.getInstance().decrypt(appId);
CommunicationFragment.service.setAddress(serverAddress);
CommunicationFragment.service.setTenantID(tenant);
CommunicationFragment.service.setClientID(appId);
CommunicationFragment.service.setUserID(getUserName());
CommunicationFragment.contextAttributes.set("email", email);
CommunicationFragment.contextAttributes.set("fullName", fullName);
CommunicationFragment.contextAttributes.set("phone", phoneNumber);
final ClientInfo clientInfo = new ClientInfo(CommunicationFragment.service.getTenantID(),phone,CommunicationFragment.service.getApplicationID(),token);
CommunicationFragment.setClientInfo(clientInfo, MceDemoApplication.this.getApplicationContext());
}
});
}
}
public class HomeActivity extends AppCompatActivity implements TaskCompletionNotifier, NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate...");
if (getIntent().getExtras() != null
&& getIntent().getExtras().getBoolean(CommunicationFragment.IS_IN_APP_CALL_RINGING)) {
initCxFragment();
}
...
}
private void initCxFragment() {
final FragmentManager manager = getSupportFragmentManager();
mCxFragment = (CommunicationFragment) manager.findFragmentByTag(FRAGMENT_TAG);
if (mCxFragment == null) {
mCxFragment = CommunicationFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.cx_container, mCxFragment, FRAGMENT_TAG)
.commit();
}
mCxFragment.setNotificationHandler(((MceDemoApplication) getApplication()).getNotificationHandler());
}
}
What to do next
Next, see Additional Configuration Options for your Android App.