Android GCM PushNotification - Add a custom sound file to the application.

I get GCM Push Notification successfully. Now I want to add a custom sound file instead of the default sound. I tried with Uri from

File: ///res/raw/pop.mp3

in

Notification.DEFAULT_SOUND;

but not success. Please share if you have a better solution.

The following is the code for the GCMIntentService.java method -

/** * Issues a notification to inform the user that server has sent a message. */ private static void generateNotification(Context context, String message) { System.out.println("Called generateNotification>>>>>>>>>>>>>"+message); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); // Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context) .setSmallIcon(R.drawable.app_icon) .setContentTitle(title) .setStyle( new NotificationCompat.BigTextStyle().bigText(message)) .setContentText(message); Intent notificationIntent = new Intent(context, SplashActivity.class); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); mBuilder.setContentIntent(intent); Notification notification = mBuilder.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); } 
+8
android uri push-notification google-cloud-messaging
source share
1 answer

To add custom sound add this

 notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop); 

i.e. When changing the code

 notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; 

to

 notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop); notification.defaults |= Notification.DEFAULT_VIBRATE; 
+15
source share

All Articles