Android Integration: Version 2.1.00
App Configuration
Set the Vibes app ID in your AndroidManifest.xml, inside your block. You can retrieve your app ID by contacting your Vibes Account Manager.
<meta-data android:name="vibes_app_id" android:value="vibesAppId" />
If you want to update the default Vibes endpoint (to point to a different Vibes instance) you can specify the following in your AndroidManifest.xml:
<!-- WARNING: This is only if you want to change the default Vibes endpoint -->
<meta-data android:name="vibes_api_url" android:value="${vibesAppUrl}" />
In your build.gradle (app):
android {
defaultConfig {
manifestPlaceholders = \[vibesAppId:"[YOUR APP ID]"]
}
...
}\
or if you want to change the Vibes endpoint:
android {
defaultConfig {
manifestPlaceholders = \[vibesAppId: "[YOUR APP ID]",
vibesAppUrl: "[Vibes non Default endpoint]"]`
` }
...
}\
You must reset the Vibes default endpoint if you want to use the Vibes Platform Europe instance, as defined in Technical Details.
- Default Vibes Push endpoint (US): https://public-api.vibescm.com/mobile_apps
- Vibes Push Europe endpoint (UK): https://public-api.vibescmeurope.com/mobile_apps
Registering a Device
Use the following code to register a device. You can add the code wherever it is most appropriate for your application. You may find that an onCreate for your main Activity or custom Application-subclass is the best place.
// NOTE: the `VibesListener` is optional; you can ignore it if you don't care
// about the result of the registration.
Vibes.getInstance().registerDevice(new VibesListener<Credential>() {
@Override
public void onSuccess(Credential value) {
}
@Override
public void onFailure(String errorText) {
}
});
Unregistering a Device
Use the following code to unregister a device. You can add the code wherever it makes the most sense for your application:
// NOTE: the `VibesListener` is optional; you can ignore it if you don't care
// about the result of the unregistration.
Vibes.getInstance().unregisterDevice(new VibesListener<Void>() {
@Override
public void onSuccess(Void value) {
}
@Override
public void onFailure(String errorText) {
}
});
Push Messaging
Registering for Push
Use the following code to register for push. You can add it wherever it makes the most sense for your application:
// NOTE: the `VibesListener` is optional; you can ignore it if you don't care
// about the result of the unregistration.
String pushToken = FirebaseInstanceId.getInstance().getToken();
Vibes.getInstance().registerPush(pushToken, new VibesListener<Void>() {
@Override
public void onSuccess(Void value) {
}
@Override
public void onFailure(String errorText) {
}
});
Unregistering for Push
Use the following code to unregister for push. You can add it wherever it makes the most sense for your application:
// NOTE: the `VibesListener` is optional; you can ignore it if you don't care
// about the result of the unregistration.
Vibes.getInstance().unregisterPush(new VibesListener<Void>() {
@Override
public void onSuccess(Void value) {
}
@Override
public void onFailure(String errorText) {
}
});
Update the Device Location
Use the following code to update the location. You can add the code wherever it makes the most sense for your application. It is not required and stores the current location with device.
Vibes.getInstance().updateDeviceLatLong(double latitude, double longitude, new VibesListener<Credential>() {
@Override
public void onSuccess(Credential credential) {
...
}
@Override
public void onFailure(String s) {
...
}
});
Event Tracking
The events are automatically triggered by the SDK. If a device is not registered, the events will be stored locally and the next time the device is registered, the events will be sent to the Vibes back end. Nothing needs to be configured.
Events currently available:
- launch
- clickthru
- pushrecieved
Deep Link
A deep link is a link to an activity within the application other than its top-level. Any activity that is below the top activity in a hierarchy is thought of as deep, and a direct link to such an activity is a deep link.
Payload
The following is the payload that is sent as part of the notification. You can use the PushPayloadParser to extract the body, title, deep link and the remote message content as a hash map.
{
"to": "device token",
"data": {
"message_uid": "011ef11b-1d01-de1e-a111-11cfa1d1111a",
"body": "You did it! 🙌",
"title":"Push Notification!"
"client_app_data": {
...
"deep_link": "XXXXXXX",
...
}
}
}
Optional element
The client_app_data element is optional and will only be present if the deep_link is populated. The deep_link element will be hard coded in Campaign Manager and only the value will be modifiable.
You can pass in a key value pair as part of the client_app_data and parse the deep link information. Based on this information, you can decide which activity you want to start.
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"sound":"sound.filename",
"client_app_data":{
"deep_link":"text_in_CM_deep_link_field(example:android.intent.action.VIEW)",
},
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}
The following is one way to do this:
public class FMS extends FirebaseMessagingService {
/\*\*
_ @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
_/
@Override
public void onMessageReceived(RemoteMessage message) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
Intent intent = new Intent(this, MainActivity.class);
if (pushModel.getDeepLink() != null) {
intent = new Intent(this, DeeplinkActivity.class);
intent.putExtra(Vibes.VIBES_REMOTE_MESSAGE_DATA, pushModel.getMap());
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
builder.setContentTitle(pushModel.getTitle())
.setContentText(pushModel.getBody())
.setSmallIcon(R.drawable.firebase_icon)
.setAutoCancel(true).setContentIntent(pendingIntent);
if (notificationManager != null) {
notificationManager.notify(0, builder.build());
}
}
}
Don't forget this key
Make sure to put the VIBES_REMOTE_MESSAGE_DATA key in the intent's extras. Otherwise, Vibes won't be able to detect a clickthru event.
Targeting later versions
The NotificationCompat.Builder that takes only a context as an argument is deprecated in Oreo. If you target only Oreo and above you do not need to include the else part of the conditional block.
Notification Sound
If your application contains a custom sound for push notification, you can specify this sound on the Vibes Platform. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"sound":"sound.filename",
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
if (!pushModel.isSilentPush()) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
NotificationCompat.Builder builder;
Uri soundResourceUri;
switch (pushModel.getSound()) {
case "custom_sound":
soundResourceUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);
break;
case "default":
soundResourceUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
break;
default:
soundResourceUri = null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("ID", "ID", NotificationManager.IMPORTANCE_DEFAULT);
builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
if (soundResourceUri != null) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
notificationChannel.setSound(soundResourceUri, att);
}
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
if (soundResourceUri != null) {
builder.setSound(soundResourceUri);
}
}
if (notificationManager != null) {
notificationManager.notify(0, builder.build());
}
}
}
}
Custom Properties
You can specify custom data on the Vibes Platform. The push payload received will look like the following:
In your application, you can retrieve the custom data as follows:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"client_custom_data":{
"key1":"val1",
"key2":"val2"
},
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
JSONObject customClientData = pushModel.getCustomClientData();
if (customClientData != null) {
// Do something with the custom keys values.
}
}
}
Notification Channel
With Android O, you have to define a notification channel when you create the local notification. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"notification_channel":"default_channel",
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}
In your application you can retrieve the notification_channel and create the notification as follows:
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelID = pushModel.getChannel();
NotificationChannel notificationChannel = new NotificationChannel(channelID, channelID, NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
// Use builder to create notification
} else {
// Notification channels are not available before Oreo
}
}
Priority
With Android O, you can define the notification priority when you create the local notification. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"notification_channel":"thisismynotificationchannel",
"priority":"normal", // or "high"
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}
The two possible values for priority are Normal and High.
In your application you can retrieve the priority and create the notification as follows:
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
NotificationManager notificationManager = getSystemService(NotificationManager.class);
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelID = pushModel.getChannel();
int importance;
if (pushModel.getPriority() != null) {
importance = pushModel.getPriority();
} else {
importance = NotificationManager.IMPORTANCE_DEFAULT;
}
NotificationChannel notificationChannel = new NotificationChannel(channelID, channelID, importance);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
builder = new NotificationCompat.Builder(getBaseContext(), notificationChannel.getId());
} else {
builder = new NotificationCompat.Builder(getBaseContext());
}
builder.setContentTitle(pushModel.getTitle())
.setContentText(pushModel.getBody())
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true);
if (notificationManager != null) {
notificationManager.notify(0, builder.build());
}
}
}
Rich Media: Image
You can define the image URL in the Vibes Platform. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"client_app_data":{
"image_url" : "[URL to your image]",
},
"message_uid":"ftgybd1b-e473-hue8-abf6-8d7929467dhe"
}
}\
In your application you can create the rich push notification as follows:
public class FMS extends FirebaseMessagingService {
/**
* Method called to download the image used in the rich push notification.
*
* @param imageUrl: String
* @return Bitmap
*/
private Bitmap getBitmapFromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();
return BitmapFactory.decodeStream(in);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(getBaseContext(), "channelId");
} else {
builder = new NotificationCompat.Builder(getBaseContext());
}
if (pushModel.getRichPushMediaURL() != null) {
Bitmap imageDownloaded = getBitmapFromUrl(pushModel.getRichPushMediaURL());
builder.setLargeIcon(imageDownloaded)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(imageDownloaded));
}
}
}
Silent Push
On the Vibes Platform, you can specify to send a push notification as a silent push. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
...
"silent_push": "true",
...
"client_custom_data":{
"key1":"val1",
"key2":"val2"
},
}
}\
In your application, you can use the following to handle the push notification data and perform some actions without interrupting the user with a notification.
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
if (pushModel.isSilentPush()) {
// Handle data from pushModel.getCustomClientData() or do some work
} else {
// Create the local notification
}
}
}
Vibes Collapse ID
On the Vibes Platform, you can choose to define a vibes_collapse_id for the push notification. If a vibes_collapse_id is received, and two push notifications are sent to a customer, the latest push notification will replace the previous push notification. The user will see only the latest push notification in the top status bar. The push payload received will look like the following:
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"body":"Vibes Push SDK",
"title":"Implementing Push is Fun",
"vibes_collapse_id":"collapseKey",
...
}
}\
In your application you can collapse push notifications with the same vibes_collapse_id as follows:
public class FMS extends FirebaseMessagingService {
/**
* @see FirebaseMessagingService#onMessageReceived(RemoteMessage)
*/
@Override
public void onMessageReceived(RemoteMessage message) {
PushPayloadParser pushModel = Vibes.getInstance().createPushPayloadParser(message.getData());
String collapseKey = pushModel.getVibesCollapseId();
int notificationId = collapseKey != null ? collapseKey.hashCode() : (int)(System.currentTimeMillis() % Integer.MAX_VALUE);
if (!pushModel.isSilentPush()) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext(), "channelID");
builder.setContentTitle(pushModel.getTitle())
.setContentText(pushModel.getBody())
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true);
if (notificationManager != null) {
notificationManager.notify(notificationId, builder.build());
}
}
}
}
Updated about 1 year ago