简体   繁体   English

Android推送通知如何播放默认声音

[英]Android push notification how to play default sound

I'm using MixPanel to send push notification and on the custom payload I add the following code: {"sound":"default"} the problem Is that no sound gets played when I receive the notification. 我正在使用MixPanel发送推送通知,并在自定义有效负载上添加以下代码:{“sound”:“default”}问题是当我收到通知时没有声音播放。 Does anyone have a solution for this? 有人有解决方案吗?

Maybe this helps found here code will look like this. 也许这有助于找到这里的代码看起来像这样。

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

In order to send notification + sound using mixpanel, you need to do the following: 要使用mixpanel发送通知+声音,您需要执行以下操作:

  • add the following code to the onCreate: 将以下代码添加到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 see it received. 从mixpanel发送通知并查看收到的通知。 This will send notification on create with default sound configured on the user's device. 这将在创建时发送通知,并在用户设备上配置默认声音。

  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

Assuming you have a declaration... 假设你有一份声明......

    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);

... variable constructed somewhere in your code, try this: ...在代码中的某处构造变量,试试这个:

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

try following code 尝试以下代码

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

The default GCMReceiver in the Mixpanel library for Android that handles incoming push notifications from Mixpanel doesn't include sounds. Android的Mixpanel库中用于处理来自Mixpanel的传入推送通知的默认GCMReceiver不包含声音。 You'll need to write your own BroadcastReceiver to process incoming messages from Mixpanel. 您需要编写自己的BroadcastReceiver来处理来自Mixpanel的传入消息。

You can take a look at Mixpanel's documentation for using the low level API at : https://mixpanel.com/help/reference/android-push-notifications#advanced - then you an apply the advice from the other answers to do anything you'd like with your custom data payload. 您可以在以下网址查看Mixpanel的文档,了解如何使用低级API: https//mixpanel.com/help/reference/android-push-notifications#advanced - 然后您应用其他答案中的建议来做任何事情喜欢自定义数据有效负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM