How to use Custom Notification Sounds on Android?
PreviousHow to fix notification delivery issues on specific Android devices?NextMaking an In-App WebView message
Last updated
public class MyApplication extends Application {
public static final String CHANNEL_WIN = "win";
public static final String CHANNEL_DEFEAT = "defeat";
public static final String SOUND_RESOURCE_WIN = "sound_win";
public static final String SOUND_RESOURCE_DEFEAT = "sound_defeat";
@Override
public void onCreate() {
super.onCreate();
// This code should be in your Application subclass' onCreate, not in your Activity's! It is required so that Batch can call your interceptor while your app is in the background
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannels(this);
setupBatchChannelInterceptor();
} else {
setupLegacyBatchSoundInterceptor();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupNotificationChannels(@NonNull Context context) {
// This sample assumes that your sounds are named "sound_win"/"sound_defeat"
// and placed in the "raw" resource folder.
AudioAttributes soundAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
@SuppressLint("WrongConstant")
NotificationChannel winChannel = new NotificationChannel(
CHANNEL_WIN,
"Win notifications",
NotificationManager.IMPORTANCE_DEFAULT);
winChannel.setSound(getUriForSoundName(context, SOUND_RESOURCE_WIN), soundAttributes);
@SuppressLint("WrongConstant")
NotificationChannel defeatChannel = new NotificationChannel(
CHANNEL_DEFEAT,
"Defeat notifications",
NotificationManager.IMPORTANCE_DEFAULT);
defeatChannel.setSound(getUriForSoundName(context, SOUND_RESOURCE_DEFEAT), soundAttributes);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(winChannel);
notificationManager.createNotificationChannel(defeatChannel);
}
@NonNull
private Uri getUriForSoundName(@NonNull Context context, @NonNull String soundName) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName()
+ "/raw/" + soundName);
}
private void setupBatchChannelInterceptor() {
Batch.Push.getChannelsManager().setChannelIdInterceptor((payload, deductedChannelId) -> {
// custom payload: {"channel":"win"}/{"channel":"defeat"}
String channel = payload.getPushBundle().getString("channel");
switch (channel) {
case "win":
return CHANNEL_WIN;
case "defeat":
return CHANNEL_DEFEAT;
}
return deductedChannelId;
});
}
private void setupLegacyBatchSoundInterceptor() {
Batch.Push.setNotificationInterceptor(new BatchNotificationInterceptor() {
@Override
public NotificationCompat.Builder getPushNotificationCompatBuilder(@NonNull Context context,
@NonNull NotificationCompat.Builder defaultBuilder,
@NonNull Bundle pushIntentExtras,
int notificationId) {
String sound = pushIntentExtras.getString("channel", null);
Uri soundUri = null;
switch (sound) {
case "win":
soundUri = getUriForSoundName(context, SOUND_RESOURCE_WIN);
break;
case "defeat":
soundUri = getUriForSoundName(context, SOUND_RESOURCE_DEFEAT);
break;
}
if (soundUri != null) {
defaultBuilder.setSound(soundUri);
}
return defaultBuilder;
}
});
}
}