Tj5
Tj5

Reputation: 51

Notification not making sound or vibration?

I am making a program and I want the app to make a notification. When the notification goes off then only the ticker text is displayed. No sound or vibration or light is accompanied with 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);

Upvotes: 5

Views: 4171

Answers (4)

JasonChiu
JasonChiu

Reputation: 171

I find there is a setDefaults method in NotificationCompat.Builder , and it provides you for adjusting notification preferences. 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 don't use all default values, you can also try DEFAULT_SOUND, DEFAULT_VIBRATE,DEFAULT_LIGHTS as you want.

Upvotes: 0

Anton Cherkashyn
Anton Cherkashyn

Reputation: 5859

To add notification light you need to add this(notice the .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 default vibration pattern:

notification.defaults |= Notification.DEFAULT_VIBRATE;

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

Upvotes: 8

Dean Thomas
Dean Thomas

Reputation: 1116

To make the LED work, you need to tell it 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 make the vibrate work, you need permission added to your Manifest

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

Upvotes: 3

jtt
jtt

Reputation: 13541

Did you ensure the device is not muted and actually has a notification sound picked by the user (also not silent)?

Another thing to try would be:

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

Upvotes: 1

Related Questions