pushy/pushy-cordova

Capacitor/Ionic app - cannot modify sound?

Closed this issue · 3 comments

Hello, is it possible to change the default sound on receive notification? I think there should be some function to create a channel but I did not find such function in documentation for cordova/ionic plugin... in your PushReceiver.java there is a line Pushy.setNotificationChannel(builder, context) but it does not work to change the sound, because this way was deprecated for API > 26.

pushy commented

Hi @jammin197,
Thanks for reaching out. We'd be glad to assist.

You can modify the native PushReceiver.java implementation to customize notification behavior.

Instructions vary by platform:

Ionic
Capacitor

For example, to change the sound, find the following in your PushReceiver.java:

.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

Replace with:

.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mContext.getPackageName() + "/" + R.raw.your_custom_sound))

Please let us know if there's anything else we can help with.

Yes, this I tried.... as I wrote, there are some changes after API 26. Try it in your project and I am sure, you will not be able to play your custom sound. Anyway I do not have access to "R" in my capacitor project. But this should work also:
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/new_order"))
The sound is loaded same way for standard push notifications... and works. So there is no problem with loading but implementation itself. Please try to reproduce on your side. Try to play some custom sound.

pushy commented

Hi @jammin197,
Correct, after API level 26, a notification channel needs to be created specifying the sound URI.

Please use the following code instead of Pushy.setNotificationChannel(builder, context);:

// Get an instance of the NotificationManager service
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

// Create notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel("pushy_custom", "Push notifications", NotificationManager.IMPORTANCE_HIGH);
    mChannel.setLightColor(Color.RED);
    mChannel.enableLights(true);
    mChannel.enableVibration(true);
    mChannel.setDescription("Push notifications");
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .build();
        mChannel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.SOUND_FILENAME), audioAttributes);

    notificationManager.createNotificationChannel(mChannel);
}

// Prepare a notification with vibration, sound and lights
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "pushy_custom")
        .setAutoCancel(true)
        .setSmallIcon(android.R.drawable.ic_dialog_info)
        .setContentTitle(notificationTitle)
        .setContentText(notificationText)
        .setLights(Color.RED, 1000, 1000)
        .setVibrate(new long[]{0, 400, 250, 400})
        .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));

// Build the notification and display it
notificationManager.notify(1, builder.build());

Please let us know if there's anything else we can help with.