Notification Builder

On Android, the TUNE SDK provides a TuneNotificationBuilder class that lets you customize the appearance and behavior of the push notifications from TUNE.

Usage

Start by creating a TuneNotificationBuilder object, passing in a resource id to an icon.

TuneNotificationBuilder builder = new TuneNotificationBuilder(R.drawable.your_icon);

If you support versions before and after Lollipop, you may want to use different icons depending on the version. Starting from Lollipop, push notifications only display the alpha of the icon on a gray background.

In such a case you can do:

int smallIcon;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    smallIcon = R.drawable.your_alpha_icon;
} else {
    smallIcon = R.drawable.your_icon;
}
TuneNotificationBuilder builder = new TuneNotificationBuilder(smallIcon);

Finally, tell the TUNE SDK to use the builder you've created. This call should go after Tune.init:

Tune.getInstance().setPushNotificationBuilder(builder);

You're set! Any TUNE push notifications you receive in your app will use this builder to display the notification.

Customization

Aside from the icon, the TuneNotificationBuilder also allows you to customize the following fields:

setLargeIcon

Sets the large icon that is shown in TUNE push notifications.

This is only visible when the user expands the tray and will replace the small icon. However, on APIs before Lollipop, the small icon is still displayed on the far right of the notification. On later APIs, the small icon is super imposed at the bottom right of the large icon.

builder.setLargeIcon(R.drawable.your_large_icon);

setGroup

Sets the group that TUNE notifications should be a part of. Grouped notifications may display in a cluster or stack on devices which support such rendering.

Grouping is only supported on devices running Android KitKat and newer.

builder.setGroup("key_to_group_on");

setColor

Sets the accent color for TUNE push notifications.

Color is only displayed on devices running Lollipop and newer.

builder.setColor(Color.BLUE);

setVisibility

Sets the visibility level for TUNE push notifications.

Visibility level is one of VISIBILITY_PRIVATE (the default), VISIBILITY_PUBLIC, or VISIBILITY_SECRET.

Visibility is only supported on devices running Lollipop and newer.

builder.setVisibility(Notification.VISIBILITY_SECRET);

setSound

Sets the sound to be played when a TUNE push notification is received. If not set, the default system sound will be used.

Will override setNoSound if called after.

// Set the sound from a raw sound file in your project
builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_sound_file));

setVibrate

Sets the vibration pattern to be played when a TUNE push notification is received. If not set, the default system vibrate pattern will be used.

Will override setNoVibrate if called after.

This method requires your AndroidManifest.xml to hold the permission VIBRATE:

<uses-permission android:name="android.permission.VIBRATE" />

Pass in an array of longs that are the durations for which to turn on or off the vibrate in milliseconds.

  • The first value indicates the number of milliseconds to wait before turning the vibrate on.
  • The next value indicates the number of milliseconds to keep the vibrate on before turning it off.
  • Subsequent values alternate between durations in milliseconds to turn the vibrate off or on.
// Vibrate with 0 delay for 2s, then stop for 1s, then vibrate 1s, then stop 1s
builder.setVibrate(new long[]{0, 2000, 1000, 1000, 1000});

setOnlyAlertOnce

Sets the only alert once setting for TUNE push notifications.

For notifications with sound and vibration set, this setting determines whether the sound and vibration get played every time, or only once if the notification is already showing.

If set to true, then receiving another TUNE push notification while one is showing will not play the sound and vibration again.

builder.setOnlyAlertOnce(true);

setNoSound

Sets that no sound should be played when a TUNE push notification is received.

Will override setSound if called after.

builder.setNoSound();

setNoVibrate

Sets that no vibration should be played when a TUNE push notification is received.

Will override setVibrate if called after.

builder.setNoVibrate();

No Comments

Leave a reply