java.lang.SecurityException: Requires VIBRATE permission on Jelly Bean 4.2
Asked Answered
A

3

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

Asiatic answered 28/11, 2012 at 9:48 Comment(6)
What is the exception or issue ? . Try using permission in Manifest.Riata
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.2Asiatic
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....Reflexive
@Asiatic - Just got a crash report from the market, where it happened a SGS4 (4.3). I don't set vibration either.Araroba
Also, this haven't been reported up until now. Weird.Araroba
See also #13159886Resent
M
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.

Militate answered 5/12, 2012 at 4:39 Comment(2)
Just got a crash report from the market, where it happened a SGS4 (4.3). I don't set vibration either.Araroba
Same thing here, it does not seem to be fixed on 4.3 either.Unmanly
R
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();
    }
Receive answered 3/1, 2014 at 7:13 Comment(3)
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.Hog
@Hog 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 needReceive
I think solution 1 (adding permission in AndroidManifest.xml file) should be enoughEquivalence
H
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.

Hoyt answered 14/3, 2015 at 5:47 Comment(5)
can you confirm that above declaration with android:maxSdkVersion="18" attr will let install app on devices without Vibrator? because line with only <uses-permission android:name="android.permission.VIBRATE" /> causes app (update) not availableResa
sorry, I don't know.Hoyt
@Resa did you ever check to see if this fix works on devices without vibration?Bollen
I don't know, but in terms of allowing installation, I believe only uses-feature (not uses-permission) affects that.Hoyt
@Sorin I'm confirming Marks comment, fix works as it uses uses-permission, not uses-featureResa
M
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.

Militate answered 5/12, 2012 at 4:39 Comment(2)
Just got a crash report from the market, where it happened a SGS4 (4.3). I don't set vibration either.Araroba
Same thing here, it does not seem to be fixed on 4.3 either.Unmanly

© 2022 - 2024 — McMap. All rights reserved.