Notification does not sound or vibration?

I am making a program and I want the application to send a notification. When the notification is turned off, only the text of the ticker is displayed. No sound, vibration or light is accompanied by it.

Here is an example of my code:

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);
+5
source share
4 answers

To add notification highlighting, you need to add this (note .flags, not .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

For default sound:

notification.defaults |= Notification.DEFAULT_SOUND;

For the default vibration pattern:

notification.defaults |= Notification.DEFAULT_VIBRATE;

For vibration, as Dean Thomas said, you need permission <uses-permission android:name="android.permission.VIBRATE"/>

+8
source

To make the LED work, you need to say what to do.

   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?

To do vibrating work, you need permission added to your manifest

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

, , ( )?

, :

[Note obj].sound = value
[Note obj].LEDARGB = value
[Note obj].vibrate = value
+1
source

I find that there is a setDefaultsmethod in NotificationCompat.Builder, and it provides you with the ability to configure notifications. Here is an example:

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());

If you do not use the default values, you can also try DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTShow you want.

0
source

All Articles