2

I am using Push Notification in my app. i am able to play the default sound for push . Now i want to use some Mp3. I don't where to place mp3 in project and how to use it in activity . please help me .

Gaurav kumar
  • 796
  • 2
  • 8
  • 25

2 Answers2

3

Put the file in raw folder.

If you want to use .ogg file use this :

Thread t = new Thread()
        {
                public void run()
                {
                    MediaPlayer player = null;

                    player = MediaPlayer.create(context,R.raw.push_sound);
                    player.start();

                    try 
                    {
                        Thread.sleep(player.getDuration()+100);
                    } 
                    catch (InterruptedException e) 
                    {

                    }
                }
            }
        };
        t.start();   

EDIT:

Please use below code when you get notification in BroadcastReceiver, then call activity in that activity class.

Use below code so play sound file.

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

You have to get the help of a IntentReceiver:

public class IntentReceiver extends BroadcastReceiver {

    private static final String logTag = "PushSample";
    public static String APID_UPDATED_ACTION_SUFFIX = ".apid.updated";
    public static String gcmId="";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();

        if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {

            int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);

            // Id
            String ap_id = intent.getStringExtra(PushManager.EXTRA_APID);

            System.out.println("IntentReceiver::- ID::-" + ap_id);

            Log.i(logTag,
                    "Received push notification. Alert: "
                            + intent.getStringExtra(PushManager.EXTRA_ALERT)
                            + " [NotificationID=" + id + "]");

            logPushExtras(intent);

        } 
}

PushManager.ACTION_PUSH_RECEIVED gets fired when a push is received. You need declare the IntentReceiver in the manifest.

This is a good tutorial to follow: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69