簡體   English   中英

Android通知不會發出聲音或振動

[英]Android notifications not making sound or vibration

int icon = R.drawable.icon;  
Context context = getApplicationContext();    
CharSequence contentTitle = "My notification";  
CharSequence contentText = "Countdown Complete!";     
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent();
Notification notification = new Notification(icon, "is completed!", System.currentTimeMillis());  
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND; 
notificationManager.notify(myCountDown.getId(), notification);

這個代碼在我的Android應用程序中,我收到通知但沒有聲音或振動。

我已經在多部手機上進行了測試,聲音和振動都在設置中打開和關閉。 我也確定我要求在android清單中使用振動許可但我仍然只收到通知......

我也嘗試過:

notification.defaults = Notification.DEFAULT_ALL;

notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;

如何在通知中獲得聲音和振動?

不要忘記啟用鈴聲和通知的振動設置。 轉到設置 - >聲音。 檢查“聲音振動”。

notification.defaults | = Notification.DEFAULT_VIBRATE; 不起作用?

步驟1:確保清單中具有VIBRATE權限,並且您正在具有振動電機的設備上運行。

步驟#2:擺脫getApplicationContext() ,因為這里不需要它。

步驟3:嘗試通過Notification上的sound數據成員指定實際的鈴聲,看看是否默認不播放(你需要在flags沒有DEFAULT_SOUND )。

Notification notif ... //create your notification

notif.defaults | = Notification.DEFAULT_SOUND; notif.defaults | = Notification.DEFAULT_VIBRATE;

不要忘記在清單中包含Vibrate權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM