Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 304
Ste Avatar asked Nov 28 '12 09:11

Ste


3 Answers

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();
    }
like image 191
swiftBoy Avatar answered Oct 04 '22 05:10

swiftBoy


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.

like image 45
Mark Avatar answered Oct 04 '22 04:10

Mark


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.

like image 42
dsandler Avatar answered Oct 04 '22 04:10

dsandler