[英]Notification sound not playing
我知道這個問題已被多次詢問,但我仍然遇到問題。
在我的應用程序中,我創建了一個通知來提示用戶某些事件,並且我想播放已在應用程序中打包為原始資源的聲音。
我創建了我的通知實例:
Notification notification = new Notification(
R.drawable.some_image,
"some meaningful name",
System.currentTimeMillis() );
notification.setLatestEventInfo(...)
然后在該通知上設置一些屬性:
notification.sound = Uri.parse( "android.resource://com.my.package/" + R.raw.some_mp3_file );
notification.flags = Notification.FLAG_INSISTENT;
最后,我調用NotificationManager來顯示通知:
notificationManager.notify( 1, notification );
通知確實顯示,但聲音不播放。
難道我做錯了什么? 是否有<uses-permission>我錯過了? 我看不出任何與其他似乎已經有效的人做過不同的事情。
對於它的價值,我正在Nexus 7上直接測試。
試着看下面的代碼。 可以幫助你解決你的問題。
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.update;
CharSequence tickerText = "assignments";
long when = System.currentTimeMillis();
Notification assignmentNotification = new Notification(icon, tickerText, when);
**For sound** assignmentNotification.defaults |= Notification.DEFAULT_SOUND;
long[] vibrate = {0,100,200,300};
**For vibrate** assignmentNotification.vibrate = vibrate;
Context context = getApplicationContext();
CharSequence contentTitle = "check assignments";
CharSequence contentText = "chek ur app for assignments ";
Intent notificationIntent = new Intent(context, ViewAssignmentnotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0);
assignmentNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
** final int id = 2;
請看這個答案 ; 它告訴你,你必須明確指定notification.sound來播放這個音調;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.