2012-01-26 83 views
2
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通知不会发出声音或振动

我已经在多个电话上测试过,所有声音和振动都在设置中打开并打开。我还相信,我所要求使用的振动允许在Android清单,但我仍然只能得到通知...

我也试过:

notification.defaults = Notification.DEFAULT_ALL; 

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

如何在通知中获得声音和振动?

回答

2

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

+0

很抱歉忘记了这一点,但我试过Notification.DEFAULTS_ALL和VIBRATE,但仍然没有任何... – vbman11 2012-01-26 22:45:19

+0

和我刚刚编辑添加这些 – vbman11 2012-01-26 23:03:22

+0

您尝试创建您的意图,如在文档中Intent notificationIntent = new Intent (this,MyClass.class); – 2012-01-27 16:20:24

1

步骤#1:确保您在清单中拥有VIBRATE权限,并且您正在具有振动电机的设备上运行。

步骤#2:摆脱getApplicationContext(),因为你在这里不需要它。

第3步:尝试通过sound数据成员Notification指定实际铃声,看它是否是默认的不打的问题(你需要没有DEFAULT_SOUNDflags)。

+0

步骤#1:好友我在我的问题中说过“我也确定我要求使用Android清单中的振动许可”...并且我确实说了电话... – vbman11 2012-01-26 23:44:31

+0

第2步:获取摆脱它?将它设置为null?或删除参数?...既没有工作...空给我运行时异常,没有arg不编译... – vbman11 2012-01-26 23:46:22

+0

步骤#3:我使用DEFAULT_SOUND,因为我想让它播放默认声音.. 。我仍然应该得到振动... – vbman11 2012-01-26 23:48:49

5

不要忘记为铃声和通知启用振动设置。进入设置 - >声音。检查“发出振动”。

+0

这是用户常犯的错误...它发生在我身上 – Bachask8 2013-11-07 21:56:17

0
Notification notif ... //create your notification 

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

不要忘记在清单中包含振动权限。