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



我使用MixPanel发送推送通知,并在自定义负载上添加以下代码:{"声音":"默认"}问题是,当我收到通知时,不会播放任何声音。有人能解决这个问题吗?

也许这有助于在这里找到代码,代码会是这样的。

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

为了使用mixtpanel发送通知+声音,您需要执行以下操作:

  • 将以下代码添加到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();
    
  • 从混合面板发送通知并查看收到的通知。这将在用户设备上配置默认声音的创建时发送通知。

尝试以下代码

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

假设您有一个声明。。。

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

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

您可以查看Mixpanel的文档,了解如何使用低级API:https://mixpanel.com/help/reference/android-push-notifications#advanced-然后你可以应用其他答案中的建议,对你的自定义数据负载做任何你想做的事情。

最新更新