Android notifications do not generate 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 is in the Android app and I get a notification, but no sound or vibration.

I tested several phones with sound and vibration enabled and enabled in settings. I also made sure that I am asking to use vibration permission in the android manifest, but I still get the notification ...

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?

+2


source to share


4 answers


Don't forget to enable vibration setting for ringtone and notifications. Go to Settings โ†’ Sound. Check "Vibration Sound".



+5


source


notification.defaults | = Notification.DEFAULT_VIBRATE; does not work?



+2


source


Step # 1. Make sure that the manifest has permission VIBRATE

and that you are working on a device with a vibration motor.

Step # 2: get rid of it getApplicationContext()

, as you don't need it.

Step # 3: Try specifying the actual ringtone via item sound

on Notification

to make sure the question isn't playing by default (you don't need to have DEFAULT_SOUND

in flags

).

+1


source


Notification notif ... //create your notification

      

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

Don't forget to include Vibration permission in your manifest.

0


source







All Articles