Android Notification Builder: how to set up Sound so that sound plays in the looper

I am implementing a notification as shown below. The default sound sounds normal, but only once. All I want is to play it several times until the response is logged.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("I Am Fine") .setStyle(new NotificationCompat.BigTextStyle() .bigText(NOTIFICATION_MESSAGE)) .setContentText(NOTIFICATION_MESSAGE) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); mBuilder.setSound(alarmSound,AudioManager.STREAM_MUSIC); 

The second setSound parameter has no effect. Please, help!

+5
source share
4 answers

You must use FLAG_INSISTENT for notification. From the document: -

public static final int FLAG_INSISTENT

A bit for bitwise input in the flag field, which, if set, will sound repeated until the notification is canceled or the notification window is opened.

Constant value: 4 (0x00000004)

  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_RINGTONE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( getApplicationContext()).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("title").setContentText("message") .setSound(soundUri); // This sets the sound to play Notification mNotification = mBuilder.build(); mNotification.flags |= Notification.FLAG_INSISTENT; // Display notification notificationManager.notify(1, mNotification); 
+10
source

I called it in the SendNotification method method.

 try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); PlaySound(this,notification); } catch (Exception e) { e.printStackTrace(); } 

The definition of PlaySound is as follows:

 private void PlaySound(Context context,Uri alert){ try{ mPlayer=new MediaPlayer(); mPlayer.setDataSource(context,alert); final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE); if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0); { mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.prepare(); mPlayer.setLooping(true); mPlayer.start(); } }catch(IOException e) { Log.i("AlaramReciever", "no audio file"); } } 

To stop the sound when listening to the notification, I changed the MediaPlayer object to an open static object, as shown below:

 if(GCMNotificationIntentService.mPlayer != null) { try{ GCMNotificationIntentService.mPlayer.stop(); GCMNotificationIntentService.mPlayer.release(); }finally { GCMNotificationIntentService.mPlayer = null; } } 

Desired functionality achieved, but I keep getting the next RunTimeException

The android.media.MediaPlayer $ EventHandler handler, which sends a message to the handler in a dead thread, when it immediately calls onDestroy. Is there any way to prevent this?

0
source
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)); MediaPlayer player = MediaPlayer.create(this, notification); player.setAudioStreamType(AudioManager.STREAM_ALARM); player.setLooping(true); player.start(); 

player.setLooping(true); This will allow the media player to play the sound repeatedly. on onclick() buttons use player.stop(); to stop the sound

0
source

You need to mute the notification sound and simply play the sound file from your application using the media player. And install the media player instance in the loop.

After that, you should add a pending intent variable with your notification, as soon as the user closes the notification, the corresponding event will be fired, and you can stop the media player instance here.

-2
source

Source: https://habr.com/ru/post/1205776/


All Articles