如何解决启动通知时需要android.permission.READ_EXTERNAL_STORAGE?

时间:2017-03-27 12:15:36

标签: android push-notification android-permissions

我有一个推送通知接收器,收到后,会将通知设置为以下功能。

private void sendNotification(String message, int id, String title) {
    PendingIntent pendingIntent =
            IntentUtil.getSplashActivityNotificationPendingIntent(
                    this, ConstantValue.GCM_PUSH_ACTIVITY, id, title);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.praiseiconwhite)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(ConstantValue.GCM_PUSH_NOTIFICATION, notificationBuilder.build());
}

过去使用GCM,现在改为FCM。最近,我收到了Fabric声明requires android.permission.READ_EXTERNAL_STORAGE

的崩溃报告
Fatal Exception: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri 
content://media/external/audio/media/1266 from pid=23488, uid=10157 
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
   at android.os.Parcel.readException(Parcel.java:1546)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
   at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153)
   at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:691)
   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1113)
   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:937)
   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:864)
   at android.media.RingtoneManager.isRingtoneExist(RingtoneManager.java:1092)
   at android.app.NotificationManager.notify(NotificationManager.java:157)
   at android.app.NotificationManager.notify(NotificationManager.java:123)
   at com.mypackage.service.CustomFirebaseMessagingService.sendNotification(SourceFile:77)
   at com.mypackage.service.CustomFirebaseMessagingService.onMessageReceived(SourceFile:52)
   at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
   at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
   at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
   at com.google.firebase.iid.zzb$2.run(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:818)

我知道https://stackoverflow.com/a/32617585/3286489中的答案是要求我们申请许可。但是,这是在收到推送通知以设置通知时,此时无法进行用户交互。

此外,我的代码不是需要许可,而是在设备特定代码中

at android.os.Parcel.readException(Parcel.java:1546)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

代码清楚地说明了许可问题。

按照以下步骤操作:

1-检查您是否在清单文件中声明了READ_EXTERNAL_STORAGE的权限,如果是,那么它应该适用于所有设备,直到Lollipop

2-对于Lollipop版本搜索Runtime权限示例后的权限,虽然我添加链接以便一目了然但是您最好仔细阅读该主题,从现在开始对您有用。

Android Runtime Permission

最好的运气!