35

昨日から Android 4.2 に問題があり、プッシュ通知を受信すると、振動するように設定していなくても許可が必要になります

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

例外は nm.notify によって発生します

この問題は 2 つの異なるアプリで発生しており、コードを変更することはありません

4

3 に答える 3

41

Jelly Bean 4.1.2 で同じ例外が発生し、これを解決するために行った変更に従ってください。

1.マニフェスト ファイルに権限を追加しました。

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

2.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();
    }
于 2014-01-03T07:13:52.357 に答える
31

このバグは Android 4.2 および 4.3 でのみ発生するため、これを回避策として使用できます (つまり、maxSdkVersion を含めます)。

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

注: maxSdkVersion 属性は、API レベル 19 でのみ追加されました。この場合、幸いなことに、これはまさに必要な最小値です! 理論的には、<= 18 の任意の値を設定して同じ効果を得ることができますが、それは厄介なことです。

于 2015-03-14T05:47:39.097 に答える
26

これは、通知バイブレーション ポリシーの変更による Android 4.2 のバグです。権限のバグは、4.2.1 でのこの変更によって修正されました。

于 2012-12-05T04:39:57.853 に答える