tonyofrancis / Fetch

The best file downloader library for Android
https://www.meta.stackoverflow.com/tags/fetch2
Apache License 2.0
1.64k stars 338 forks source link

Issue Crash on Notification on Android 12 Behavior Changes (Pending Intent Flag) #615

Open JosephSanjaya opened 2 years ago

JosephSanjaya commented 2 years ago

Library crash when building pending intent for notification

Expected Behavior

Application not crashing when using DefaultFetchNotificationManager

Current Behavior

Crash catch stack:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
    at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
    at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
    at com.tonyodev.fetch2.DefaultFetchNotificationManager.getActionPendingIntent(DefaultFetchNotificationManager.kt:168)
    at com.tonyodev.fetch2.DefaultFetchNotificationManager.updateNotification(DefaultFetchNotificationManager.kt:127)
    at com.tonyodev.fetch2.DefaultFetchNotificationManager.notify(DefaultFetchNotificationManager.kt:234)
    at com.tonyodev.fetch2.DefaultFetchNotificationManager.postDownloadUpdate(DefaultFetchNotificationManager.kt:284)
com.tonyodev.fetch2.fetch.ListenerCoordinator$mainListener$1$onStarted$$inlined$synchronized$lambda$1.run(ListenerCoordinator.kt:367)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.os.HandlerThread.run(HandlerThread.java:67)

Possible Solution

Add FLAG_IMMUTABLE or FLAG_MUTABLE flag on PendingIntent

JosephSanjaya commented 2 years ago

Any solution?, my company need to release the app in 1 week, im using this to download report. can you look forward to it?

SudoDios commented 2 years ago

too

tashilapathum commented 1 year ago

This is not fixed yet so if anyone else encountered this issue, implement DefaultFetchNotificationManager, override getActionPendingIntent and just add everything already added plus the mutability flag like this:

override fun getActionPendingIntent(
        downloadNotification: DownloadNotification,
        actionType: DownloadNotification.ActionType
    ): PendingIntent {
            val intent = Intent(notificationManagerAction)
            intent.putExtra(EXTRA_NAMESPACE, downloadNotification.namespace)
            intent.putExtra(EXTRA_DOWNLOAD_ID, downloadNotification.notificationId)
            intent.putExtra(EXTRA_NOTIFICATION_ID, downloadNotification.notificationId)
            intent.putExtra(EXTRA_GROUP_ACTION, false)
            intent.putExtra(EXTRA_NOTIFICATION_GROUP_ID, downloadNotification.groupId)
            val action = when (actionType) {
                DownloadNotification.ActionType.CANCEL -> ACTION_TYPE_CANCEL
                DownloadNotification.ActionType.DELETE -> ACTION_TYPE_DELETE
                DownloadNotification.ActionType.RESUME -> ACTION_TYPE_RESUME
                DownloadNotification.ActionType.PAUSE -> ACTION_TYPE_PAUSE
                DownloadNotification.ActionType.RETRY -> ACTION_TYPE_RETRY
                else -> ACTION_TYPE_INVALID
            }
            intent.putExtra(EXTRA_ACTION_TYPE, action)

            //add the following lines to fix the issue
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                PendingIntent.getBroadcast(context, downloadNotification.notificationId + action, intent, PendingIntent.FLAG_IMMUTABLE)
            else
                PendingIntent.getBroadcast(context, downloadNotification.notificationId + action, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    }

Tested and working without any issue on Android 12+