Android push notification how to play default sound

I use MixPanel to send push notifications, and in the user payload I add the following code: {"sound": "default"} the problem is that when a notification is received, sound does not play. Does anyone have a solution for this?

+5
source share
7 answers

Perhaps this will help to find here the code will look like this.

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); 
+4
source

To send notification + sound using mixpanel, you need to do the following:

  • add the following code to onCreate:

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); 
  • Send notification from mixpanel and receive it. This will send a creation notification with standard sound configured on the user device.

+2
source

Assuming you have an ad ...

  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setAutoCancel(true) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setTicker(title) .setWhen(ts) .setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle() .bigText(message)) .setContentText(message); 

... built somewhere in your code, try the following:

 final String ringTone = "default ringtone"; // or store in preferences, and fallback to this mBuilder.setSound(Uri.parse(ringTone)); 
+1
source

try the following code

 Notification notification = new Notification(R.drawable.appicon, "Notification", System.currentTimeMillis()); notification.defaults = Notification.DEFAULT_SOUND; 
+1
source
 final Notification notification = new Notification(iconResId, tickerText, System.currentTimeMillis()); final String packageName = context.getPackageName(); notification.sound = Uri.parse("android.resource://" + packageName + "/" + soundResId); 
+1
source
  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 
+1
source

By default, the GCMReceiver in the Mixpanel library for Android, which handles incoming push notifications from Mixpanel, does not include sounds. You will need to write your own BroadcastReceiver to handle incoming messages from Mixpanel.

You can see the Mixpanel documentation for using the low-level API at: https://mixpanel.com/help/reference/android-push-notifications#advanced - then you will apply advice from the other answers to do whatever you would like using its payload.

0
source

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


All Articles