2014-09-03 137 views
5

我正在使用MixPanel發送推送通知,並在自定義有效載荷上添加了以下代碼: {「sound」:「default」}問題當沒有聲音播放時我收到通知。有沒有人有這個解決方案?Android推送通知如何播放默認聲音

回答

4

也許這有助於發現here代碼看起來像這樣。

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

是否可以在清單中設置推送通知聲音? – Pruthviraj 2016-04-29 11:03:51

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

我得到一個錯誤,說「mBuilder未初始化」如果我點擊修復按鈕Eclipse自動初始化mBuilder爲空,當我運行應用程序崩潰 – Signo 2014-09-03 07:31:55

+0

我提到「假設」你有它填充的地方。無論如何,它看起來像上面編輯的代碼。請注意,您必須設置標題,ts和消息變量! – alpinescrambler 2014-09-03 07:34:11

+0

對我來說是正確的答案(使用改編的Xamarin代碼)。完全忘了檢查Builder是否有SetSound方法!謝謝。 – nrod 2015-05-22 15:37:16

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

處理來自Mixpanel的傳入推送通知的Android的Mixpanel庫中的默認GCMReceiver不包含聲音。您需要編寫自己的BroadcastReceiver來處理來自Mixpanel的傳入消息。

您可以看看Mixpanel的文檔,以瞭解如何使用低級API:https://mixpanel.com/help/reference/android-push-notifications#advanced - 然後您可以應用其他答案中的建議來根據您的自定義數據有效載荷執行任何操作。

1
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 
2

爲了使用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(); 
    
  • 發送通知並看到它收到。這將通過用戶設備上配置的默認聲音發送創建通​​知。