36

Since yesterday I have an issue on Android 4.2 when I receive push notifications it requires the permission even if i don't set it to vibrate

Notification notification = new Notification(icon, notificationItem.message, when);
notification.setLatestEventInfo(context, "App", notificationItem.message,
            PendingIntent.getActivity(context, 0, intent, 0));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;

NotificationManager nm =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(notificationItem.notificationID, notification);

the exception is raised by nm.notify

I have this issue in two different apps and i never modify the code

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Ste
  • 2,116
  • 7
  • 24
  • 24
  • What is the exception or issue ? . Try using permission in Manifest. – Rahul Patil Nov 28 '12 at 09:52
  • 1
    i don't want to do that, I don't want the notification to vibrate, as you can see I don't set it in the defaults, again it happens only on Android 4.2 – Ste Nov 28 '12 at 10:38
  • Have the same issue. It is really a new permission that is required for 4.2 - for my part I'll simply add the vibrate permission for now, but you are right that it shold not be needed in your case. I would raise an issue.... – Sven Haiges Dec 03 '12 at 09:12
  • @Ste - Just got a crash report from the market, where it happened a SGS4 (4.3). I don't set vibration either. – AlikElzin-kilaka Feb 13 '14 at 10:13
  • Also, this haven't been reported up until now. Weird. – AlikElzin-kilaka Feb 13 '14 at 10:14
  • See also https://stackoverflow.com/questions/13159386/android-does-notification-default-vibrate-require-vibrate-permission – caw Mar 15 '21 at 06:08

3 Answers3

41

I got the same Exception in Jelly Bean 4.1.2, then following changes I made to resolve this

1.added permission in manifest file.

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

2.Notification Composing covered by Try-Catch

 try
    {
        mNotificationManager = (NotificationManager)          
        this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        this)
                .setSmallIcon(R.drawable.ic_notif_alert)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setStyle(bigTextStyle)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(true);
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            Log.d(TAG, "---- Notification Composed ----");
    }
    catch(SecurityException se)
    {
        se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • 1
    Isn't either 1 or 2 sufficient to fix this problem. I'd prefer only 1, if your users will be OK with a bit of permission creep. – Diederik Feb 26 '15 at 09:02
  • 1
    @Diederik see the answer I have shared is best practices for handling this issue in my eyes, its up-to you how you want do it, No matter 1 or 2, **app should work on all the device is the need** – swiftBoy Feb 26 '15 at 09:36
  • 2
    I think solution 1 (adding permission in AndroidManifest.xml file) should be enough – zulkarnain shah Jan 29 '16 at 03:56
32

Since this bug only occurs on Android 4.2 and 4.3 you might use this as a workaround (i.e. include the maxSdkVersion):

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

Note: the maxSdkVersion attribute was only added in API level 19, which in this case is luckily exactly the minimum we want! In theory we could put any value <= 18 to get the same effect, but that would be nasty.

Mark
  • 7,446
  • 5
  • 55
  • 75
  • 2
    can you confirm that above declaration with `android:maxSdkVersion="18"` attr will let install app on devices without Vibrator? because line with only `` causes app (update) not available – snachmsm Jul 17 '17 at 10:58
  • sorry, I don't know. – Mark Jul 18 '17 at 07:24
  • @snachmsm did you ever check to see if this fix works on devices without vibration? – Sorin Bolos Sep 10 '18 at 17:46
  • 2
    I don't know, but in terms of allowing installation, I believe only uses-feature (not uses-permission) affects that. – Mark Sep 11 '18 at 14:49
  • 1
    @Sorin I'm confirming Marks comment, fix works as it uses `uses-permission`, not `uses-feature` – snachmsm Sep 12 '18 at 06:35
26

This was a bug in Android 4.2 due to a change in the notification vibration policy; the permission bug was fixed by this change in 4.2.1.

dsandler
  • 2,471
  • 17
  • 11