Björn
Björn

Reputation: 1

Custom sound in Android Push Notifications (GCM )

How can I play a custom sound when receiving an Android Push Notification?

Within the app I can control this by using the media player. Outside the APP I can not find the correct path to the audio file . I already have "/android_assets/www/" tried "www/", but there is always the default sound when you receive a push notification outside the app.

Upvotes: 0

Views: 1637

Answers (1)

Banik
Banik

Reputation: 921

First of all, you would need to add the new sound file in raw folder under the res folder.

Since it is cordova Application,you are using Cordova push Plugin.There is a package called com.plugin.gcm.In that package open GCMIntentService.java.Under the

    public void createNotification(Context context, Bundle extras)

method ,there should be a line at the end of this method

    mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());

Just replace the line with the following codes

    Notification notification = mBuilder.build();
    notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/your_sound_file_name.mp3");
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    mNotificationManager.notify((String) appName, NOTIFICATION_ID, notification);

Upvotes: 4

Related Questions