简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM