【问题标题】:Android push notification how to play default soundAndroid推送通知如何播放默认声音
【发布时间】:2016-06-05 21:11:32
【问题描述】:

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

【问题讨论】:

    标签: android push-notification mixpanel


    【解决方案1】:

    也许这有助于找到here 代码看起来像这样。

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

    【讨论】:

      【解决方案2】:
        mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          试试下面的代码

          Notification notification = new Notification(R.drawable.appicon,
                          "Notification", System.currentTimeMillis()); 
          notification.defaults = Notification.DEFAULT_SOUND;
          

          【讨论】:

            【解决方案5】:
            final Notification notification =
                new Notification(iconResId, tickerText, System.currentTimeMillis());
            final String packageName = context.getPackageName();
            notification.sound =
                Uri.parse("android.resource://" + packageName + "/" + soundResId);
            

            【讨论】:

              【解决方案6】:

              假设你有一个声明......

                  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,并且当我运行应用程序时它会崩溃
              • 我提到“假设”你在某处填写了它。无论如何,它看起来像上面编辑过的代码。注意,你必须设置title、ts和message变量!
              • 对我来说是正确的答案(使用改编的 Xamarin 代码)。完全忘记检查 Builder 是否有 SetSound 方法!谢谢。
              【解决方案7】:

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

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

              【讨论】:

                猜你喜欢
                • 2012-05-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-02-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多