Search code examples
androidpush-notificationringtone

How to play a ringtone which is only for Push Notification Arrivals of my application?


I am working on Android, and I have implemented Push Notifications feature in my app using GCM.

And my intention is to play a ringtone from app Assets or device Sdcard when only for my Application related push Notification arrivals in the installed device.


Solution

  • 1.Create 1 folder named raw under yourApp/res/

    2.Do copy and paste your ringtone in this folder

    3.Write the below code when your application notify the notification.!

                SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    
                int iTmp = sp.load(context, R.raw.windows_8_notify, 1); // in 2nd param u have to pass your desire ringtone
    
                sp.play(iTmp, 1, 1, 0, 0, 1);
    
                MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.windows_8_notify); // in 2nd param u have to pass your desire ringtone
                //mPlayer.prepare();
                mPlayer.start();