SDK-324 - Updated way of handling notifications to not use AsyncTask#981
SDK-324 - Updated way of handling notifications to not use AsyncTask#981franco-zalamena-iterable wants to merge 6 commits intomasterfrom
Conversation
43368df to
bfd33a5
Compare
iterableapi/src/main/java/com/iterable/iterableapi/IterableFirebaseMessagingService.java
Fixed
Show fixed
Hide fixed
davidtruong
left a comment
There was a problem hiding this comment.
Please remove unnecessary logic for ghost silent notifications from the NotificationWorker.
|
|
||
| Bundle extras = IterableNotificationHelper.mapToBundle(messageData); | ||
| return IterableNotificationHelper.isGhostPush(extras); | ||
| private static void enqueueNotificationWork(@NonNull final Context context, @NonNull final Bundle extras, boolean isGhostPush) { |
There was a problem hiding this comment.
Can remove the isGhostPush boolean here since notifications only get queued for non ghost push notifications.
| * to comply with Firebase best practices. This class is kept for backwards compatibility only. | ||
| */ | ||
| @Deprecated | ||
| class IterableNotificationManager extends AsyncTask<IterableNotificationBuilder, Void, Void> { |
There was a problem hiding this comment.
Do we still need this deprecated class? Is it even visible to be used by customers?
|
|
||
| if (isGhostPush) { | ||
| IterableLogger.d(TAG, "Ghost push detected, skipping notification display") | ||
| return Result.success() |
There was a problem hiding this comment.
This seems unnecessary to create the IterableNotificationWorker only to skip the notification display. Please remove the flow of ghost/silent notifications from your IterableNotificationWorker class. If you want to log anything related to silent notifications, you can do so inside of handleMessageReceived.
The ghost push notification logic is filtered out already in IterableFirebaseMessagingService.handleMessageReceived to process silent notifications with different notificationTypes.
bfd33a5 to
49ad48c
Compare
| context.getApplicationContext(), extras); | ||
| new IterableNotificationManager().execute(notificationBuilder); | ||
|
|
||
| enqueueNotificationWork(context, extras); |
There was a problem hiding this comment.
Does this follow the exact pattern that firebase wants? Didn't it only want to schedule the notificationWork only if there was something necessary to run in the background such as image fetching. Otherwise display the notification immediately?
There was a problem hiding this comment.
great catch!
I tried replacing the async task that we had before with the new WorkScheduler 1:1, we were previously not respecting the documentation and that passed by me unnoticed.
I will update the code to follow firebase best practices
8a5f313 to
10c199b
Compare
| context.getApplicationContext(), extras); | ||
| new IterableNotificationManager().execute(notificationBuilder); | ||
|
|
||
| if (IterableNotificationHelper.hasAttachmentUrl(extras)) { |
There was a problem hiding this comment.
Verified that only the image attachment needs to schedule the NotificationWork.
Sounds are set as part of the notification category so it doesn't need to be scheduled.
There was a problem hiding this comment.
yeah from what i checked we don't need to do the sound async, only images
| private static void enqueueNotificationWork(@NonNull final Context context, @NonNull final Bundle extras) { | ||
| IterableNotificationWorkScheduler scheduler = new IterableNotificationWorkScheduler(context); | ||
|
|
||
| scheduler.scheduleNotificationWork( |
There was a problem hiding this comment.
How does the scheduler actually create the notification on device?
There was a problem hiding this comment.
How does it know the format of how to render the notification since it doesn't call the IterableNotificationHelper.createNotification function?
There was a problem hiding this comment.
If you check IterableNotificationWorker.doWork() you can see we are building the notification there, the unused createNotification apparently is there for backwards compatibility since 2019, that actually should be deprecated in my opinion, we can use this PR to do that
🔹 Jira Ticket(s) if any
✏️ Description
Migrates push notification handling from the deprecated
AsyncTaskto Android'sWorkManagerwith expedited work support.Why
AsyncTaskwas deprecated in API 30. Firebase and Android documentation explicitly recommendWorkManagerfor processing FCM messages:How
IterableFirebaseMessagingService- replacedAsyncTaskexecution withenqueueNotificationWork(), which schedules an expeditedOneTimeWorkRequestviaWorkManager. Includes a fallback that posts directly if scheduling fails.IterableNotificationWorkScheduler(new) - schedules expedited work withOutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST, ensuring notifications are always processed even when quota is exhausted.IterableNotificationWorker(new) -Workersubclass that deserializes notification data, builds the notification, and posts it. OverridesgetForegroundInfo()for pre-Android 12 compatibility.IterableNotificationHelper- added null-safety checks onextrasparameter.Since
minSdkVersionis 21, devices running Android 5–11 requiregetForegroundInfo()to avoidIllegalStateExceptionwhen running expedited work:Dependencies
androidx.work:work-runtime:2.9.0androidx.work:work-testing:2.9.0(test only)