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).
3 Answers
Answers 1
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.
Answers 2
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});
Answers 3
Try this one may be this help you out.
For Lights...
notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;
For Vibration...
long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;
0 comments:
Post a Comment