Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom notification sound not playing

I'm trying to make a custom sound play on a status bar notification. The .mp3 file is in res/raw/. But when I notify the user the sound is not played. I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer.

Here is my method:

public void showNotification()
{
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.feedback;        // icon from resources
        CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = mContext.getString(R.string.statusbar_notification);  // message title
        CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed);      // message text

        Intent notificationIntent = new Intent(mContext, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);

        //notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);
}

Thanks.

like image 499
rogcg Avatar asked Nov 07 '11 22:11

rogcg


People also ask

Why are my notification Sounds not playing?

Tap on three-dot apps in your Messages app, and go to Settings > Notifications. If not, enable Show notifications and proceed to tweak options such as Allow playing sound and Allow vibration.

Why can't I change my notification sound Samsung?

To change your Samsung phone's notification sound, go to "Sounds and vibration" in the Settings app. You can import audio by downloading sounds to your phone and using the My Files app to copy them to the Notifications folder.

Why is my Samsung not making a sound when I get a text?

Go back and apply the same settings to 'New Messages', then close the Messages app and go to Settings > Sounds and Vibration, and make sure that Sound mode is enabled.


1 Answers

From the documentation for ContentResolver:

The Uri should be one of the following formats: android.resource://package_name/id_number

You are passing the String "R.raw.notificationsound" which means nothing. Instead try this:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );
like image 69
xorgate Avatar answered Oct 13 '22 10:10

xorgate