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

6

我正在使用MixPanel发送推送通知,并在自定义负载中添加以下代码: {"sound":"default"} 问题是当我收到通知时没有声音播放。是否有人有解决方案?

7个回答

4
  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

4
为了使用Mixpanel发送通知和声音,您需要执行以下操作:
  • add the following code to the 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. This will send notification on create with default sound configured on the user's device.


4
也许这篇文章可以帮助你找到答案,代码如下:这里
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

1
假设您有一个声明...
    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);

如果您在代码中构建了一个变量,请尝试以下操作:
final String ringTone = "default ringtone"; // or store in preferences, and fallback to this
mBuilder.setSound(Uri.parse(ringTone));

我遇到了一个错误,提示“mBuilder未初始化”。如果我点击修复按钮,Eclipse会自动将mBuilder初始化为null,但当我运行应用程序时,它会崩溃。 - LS_
我提到“假设”你已经在某个地方填写了它。无论如何,它看起来像上面编辑过的代码。请注意,您必须设置标题、ts和消息变量! - alpinescrambler
对我来说这是正确的答案(使用改编后的Xamarin代码)。完全忘记检查Builder是否有SetSound方法!谢谢。 - nrod

1
尝试以下代码。
Notification notification = new Notification(R.drawable.appicon,
                "Notification", System.currentTimeMillis()); 
notification.defaults = Notification.DEFAULT_SOUND;

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

0

Android Mixpanel库中默认的GCMReceiver处理来自Mixpanel的推送通知时不包括声音。您需要编写自己的BroadcastReceiver来处理来自Mixpanel的消息。

您可以查看Mixpanel的文档,了解使用低级API的方法:https://mixpanel.com/help/reference/android-push-notifications#advanced - 然后应用其他答案中的建议,以对自定义数据有效负载执行任何操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接