简体   繁体   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);

This code in my android application and i get the notification but no sound or vibration. 这个代码在我的Android应用程序中,我收到通知但没有声音或振动。

I've tested on multiple phones all with sound and vibration turned on and up in settings. 我已经在多部手机上进行了测试,声音和振动都在设置中打开和关闭。 I've also made sure that i'm asking to use the vibration permission in the android manifest but i still only get the notification... 我也确定我要求在android清单中使用振动许可但我仍然只收到通知......

I've also tried: 我也尝试过:

notification.defaults = Notification.DEFAULT_ALL;

and

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

How do I get sound and vibration in my notifications? 如何在通知中获得声音和振动?

Don't forget to enable the vibration setting for ringtone and notifications. 不要忘记启用铃声和通知的振动设置。 Go to Settings -> Sound. 转到设置 - >声音。 Check "Sound an vibration". 检查“声音振动”。

notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults | = Notification.DEFAULT_VIBRATE; doesn't work ? 不起作用?

Step #1: Make sure you have the VIBRATE permission in the manifest, and that you are running on a device that has a vibration motor. 步骤1:确保清单中具有VIBRATE权限,并且您正在具有振动电机的设备上运行。

Step #2: Get rid of getApplicationContext() , as you do not need it here. 步骤#2:摆脱getApplicationContext() ,因为这里不需要它。

Step #3: Try specifying an actual ringtone via the sound data member on Notification , to see if it's a matter of the default not playing (you will need to not have DEFAULT_SOUND in flags ). 步骤3:尝试通过Notification上的sound数据成员指定实际的铃声,看看是否默认不播放(你需要在flags没有DEFAULT_SOUND )。

Notification notif ... //create your notification

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

Don't forget to include the Vibrate permission in your manifest. 不要忘记在清单中包含Vibrate权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM