Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Sound, Vibration and LED don't work

I'm trying to set custom Sound, Vibration and LED colors for the Notificaitons in my app - but it doesn't work. Everything else like setting the title, icon, color etc work fine. I've tried many solutions suggested in Stackoverflow but they didn't work either, so I'm asking a question.

Here is my notification code -

    Intent resultIntent = new Intent(this, ActivityB.class);
    Bundle b = new Bundle();
    //Some bundle related Code
    resultIntent.putExtra("bundle",b);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, ActivityA.class));
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_small_logo);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo_large));
    builder.setContentTitle(notification.getTitle());
    builder.setContentText(notification.getBody());
    builder.setColor(Color.parseColor("#FFFFFF"));
    builder.setStyle(new NotificationCompat.BigTextStyle());
    builder.setVibrate(new long[] { 1000, 100, 1000, 100, 1000 });
    builder.setLights(Color.YELLOW, 3000, 3000);
    builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1));
    builder.setAutoCancel(true);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

I have added permission for Vibration too. Tested this on 2 phones running Lollipop and Marshmallow.

Edit 1:

Sharing all the permissions that my application uses -

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>

Edit 2: Works on Marshmallow version phone. Does not work on Phones with Lollipop.

Edit 3: Works on Nougat too (One plus 3T Phone).

like image 207
Basanth Avatar asked Apr 09 '17 11:04

Basanth


People also ask

Why is my notification sound not working?

Check and Increase Notification Volume Level So you must check the notification volume level separately. To do so, go to Settings > Sounds and vibration > Volume. Increase the slider next to Notifications by moving it to right. Alternatively, press up or down volume buttons on the side of your phone.

Why is my Samsung not making notification sounds?

Go to settings, apps, Samsung app settings, select an app here we will select messages as an example. Select messages settings, notifications, notification categories. You will then see the notification options, tap on these and change the sound. Are you on android 12?

Why are my notifications not showing up on Android?

Cause of Notifications Not Showing up on AndroidDo Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.


1 Answers

Based on your comments, it seems to be problem with your phone itself. I was asking in (1) regarding the vibration because you are setting { 1000, 100, 1000, 100, 1000 } which is a 1000 ms delay, followed by 100 ms vibration. This can be too little to detect.

Anyway, I had the same problem for vibration on some devices, so I used vibrator service directly instead. What I did was like this after issuing the notification. As per you comment below, I also added the ringtone manager section.

// built the notification without vibration and sound
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
// start vibrator manually
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(new long[] {1000, 100, 1000, 100, 1000}, Constants.VIBRATION_ONCE);
// start sound manually
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1);
Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
ringtone.play();

This seems to work for most devices. As for the light of the notification, it is already stated in Official Document that

Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds). Not all devices will honor all (or even any) of these values.

If you are using Mi devices, try to "trust" the app and enable all permissions required for notifications, including "auto-start". It will work in most cases.

like image 86
tingyik90 Avatar answered Oct 03 '22 21:10

tingyik90