14

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).

Basanth
  • 737
  • 8
  • 16
  • `setVibrate()` will not work unless you hold the `VIBRATE` permission. `setLights()` may not work if the device does not have an LED that it uses for notifications. – CommonsWare Apr 09 '17 at 11:19
  • I have added Vibration permission, I've mentioned this in the End of the question.. I have tested on Devices that use LED for notifications.. – Basanth Apr 09 '17 at 11:20
  • try to use builder.setPriority() https://developer.android.com/reference/android/app/Notification.Builder.html#setPriority(int) – Moosa Baloch Apr 14 '17 at 07:48
  • @MoosaBaloch I set the priority to PRIORITY_HIGH and it did not work.. – Basanth Apr 15 '17 at 05:51
  • 1
    So Does .setDefaults(Notification.DEFAULT_ALL) works as expected? Try to set all those setting to default to check if your notification is built correctly.. – Keivan Esbati Apr 16 '17 at 06:06
  • Just to confirm (1) you want a 100 ms vibration with 1 second pause? or you want to have a 1 second vibration with 100 ms pause? (2) what is not working for the sound? totally no sound at all? (3) what is not working for the LED? totally no LED light or just the wrong colour? – tingyik90 Apr 16 '17 at 06:38
  • @KeivanEsbati I will check defaults and get back to you.. – Basanth Apr 16 '17 at 16:49
  • @tingyik90 1) I can experiment on vibration pattern after getting the vibration working at least 2) Totally no sound 3) no light at all – Basanth Apr 16 '17 at 16:49
  • Are you custom handling Firebase notifications? [Read this](https://firebase.google.com/docs/cloud-messaging/android/receive). They are sometimes handled by the system. That's why we use exclusively DATA notifications. /// Are you testing on a real device? Is the notification volume loud enough? Is there a notification sound set in the system? – Eugen Pechanec Apr 16 '17 at 17:06
  • @EugenPechanec I'm using Firebase notifications - the onMessageReceived way. I've tested on 2 devices with Lollipop where I get Notifications and Vibrations for Notifications from Other apps but not from my own. Also just in, My Friend tested this on a phone with Marshmallow and found out that the Vibration and Sound is working. – Basanth Apr 16 '17 at 17:19
  • Usually is the problem with the phone, espcially Mi phone. You need to go to the app settings, "trust" the app and allow notifications. – tingyik90 Apr 17 '17 at 08:56
  • @tingyik90 I use Yu Yureka and Xolo Era HD.. I've enabled notifications in the settings too. Notifications work for the other apps on the same Phones.. – Basanth Apr 17 '17 at 13:30
  • @user2450263 I've used PRIORITY_HIGH and MAX both. Dint work either.. – Basanth Apr 17 '17 at 13:31
  • set your version permission and check it. may be it will help you – parik dhakan Apr 18 '17 at 10:56

2 Answers2

8

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.

tingyik90
  • 1,641
  • 14
  • 23
  • I will test this out tonight and let you know. Although the vibration solution is little of a hack :D .. Can you also tell me how I can fix the sound problem? Apart from playing the sound myself.. – Basanth Apr 17 '17 at 13:36
  • From what I see in your code, it seems to be correct. What is the format of the sound? .mp3? Since your friend had the sound, it should not be a problem with the sound, but your phone. Those 2 phones are very uncommon. It's either (1) you write a code that work on your device but rather hacky, as I suggested for the vibrator (2) write a supposedly usable code and hope most devices will work. – tingyik90 Apr 17 '17 at 14:03
  • Yes it is mp3. I can't say that the problem is with the phone because I get notifications with Sound, Light and Vibration from other apps viz Whatsapp, Gmail, Facebook Lite etc.. But if nothing works out then I may have to follow (1) – Basanth Apr 17 '17 at 14:08
  • I've also added the code for using `RingtoneManager` – tingyik90 Apr 17 '17 at 14:13
  • Will test it out tonight and let you know – Basanth Apr 17 '17 at 14:14
0

One of the reasons the sound does not work is because it cannot find the file change from builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1)); to

String uriSound = String.format("android.resource://%s/raw/%s",getPackageName(), R.raw.notif1);

   builder.setSound(uriSound);

For the lights use RGB combinations. builder.setLights(Color.rgb(130, 130, 130));

change the vibration pattern to something more uniform,

builder.setVibrate(new long[] { 50, 100, 150, 200, 250});
Remario
  • 3,813
  • 2
  • 18
  • 25