通知不发出声音或振动?

时间:2012-01-12 22:13:14

标签: java android notifications

我正在制作一个程序,我希望该应用程序发出通知。当通知消失时,仅显示自动收报机文本。没有声音,振动或光线伴随着它。

以下是我的代码示例:

int icon = R.drawable.icon;  
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Context context = getApplicationContext();      

CharSequence contentTitle = "My notification";  
CharSequence contentText = "Countdown Complete!";

Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon, myCountDown.getName() + " is completed!", System.currentTimeMillis());

long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notificationManager.notify(myCountDown.getId(), notification);

4 个答案:

答案 0 :(得分:8)

要添加通知灯,您需要添加它(请注意.flags,而不是.defaults):

notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB=0xffffffff; //color, in this case, white
notification.ledOnMS=1000; //light on in milliseconds
notification.ledOffMS=4000; //light off in milliseconds

默认声音:

notification.defaults |= Notification.DEFAULT_SOUND;

对于默认振动模式:

notification.defaults |= Notification.DEFAULT_VIBRATE;

对于振动,如Dean Thomas所述,您需要获得许可<uses-permission android:name="android.permission.VIBRATE"/>

答案 1 :(得分:3)

要使LED正常工作,您需要告诉它该做什么。

   notification.ledOnMS = 1000; //Be on for a second
   notification.ledOffMS = 1000; //Be off for a second
   notification.ledARGB = Color.GREEN; //What colour should the LED be?

要使振动工作,您需要在您的清单中添加权限

<uses-permission android:name="android.permission.VIBRATE"/>

答案 2 :(得分:1)

您确定设备没有静音且实际上是否有用户选择的通知声音(也不是静音)?

要尝试的另一件事是:

[Note obj].sound = value
[Note obj].LEDARGB = value
[Note obj].vibrate = value

答案 3 :(得分:0)

我发现setDefaults中有一个NotificationCompat.Builder方法,它可以让您调整通知偏好设置。 这是一个例子:

NotificationManager notificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this);

builder.setSmallIcon(R.drawable.taiwanpic)
            .setWhen(System.currentTimeMillis())
            .setContentTitle("AAAAA")
            .setContentText("BBBBB")
            .setSubText("CCCCC")
            .setContentInfo("DDDDD")
            .setTicker("EEEEE")
            .setDefaults(Notification.DEFAULT_ALL);
int pid = android.os.Process.myPid();

notificationManager.notify(pid, builder.build());

如果您不使用所有默认值,也可以根据需要尝试DEFAULT_SOUNDDEFAULT_VIBRATEDEFAULT_LIGHTS