Skip to content

Commit

Permalink
Merge pull request #20168 from wordpress-mobile/fix/18495-CannotPostF…
Browse files Browse the repository at this point in the history
…oregroundServiceNotificationException

Create the transient channel used for upload notification when notifications are disabled
mkevins authored Feb 11, 2024
2 parents 85f5945 + 07d89b5 commit db61f3b
Showing 2 changed files with 78 additions and 55 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
* [**] Prevent images from temporarily disappearing when uploading media [https://github.com/WordPress/gutenberg/pull/57869]
* [***] [Jetpack-only] Reader: introduced new UI/UX for content navigation and filtering [https://github.com/wordpress-mobile/WordPress-Android/pull/19978]
* [**] Prevents crashes when the webview state is too big [https://github.com/wordpress-mobile/WordPress-Android/pull/20139]
* [*] [WordPress-only] Prevents a crash occurring when uploading videos under certain conditions [https://github.com/wordpress-mobile/WordPress-Android/pull/20168]

24.1
-----
132 changes: 77 additions & 55 deletions WordPress/src/main/java/org/wordpress/android/AppInitializer.kt
Original file line number Diff line number Diff line change
@@ -464,65 +464,78 @@ class AppInitializer @Inject constructor(
credentialsClient.connect()
}

private fun createNotificationChannelsOnSdk26() {
private fun createNotificationChannelsOnSdk26(
normal: Boolean = true,
important: Boolean = true,
reminder: Boolean = true,
transient: Boolean = true,
weeklyRoundup: Boolean = true
) {
// create Notification channels introduced in Android Oreo
if (Build.VERSION.SDK_INT >= VERSION_CODES.O) {
// Create the NORMAL channel (used for likes, comments, replies, etc.)
val normalChannel = NotificationChannel(
application.getString(R.string.notification_channel_normal_id),
application.getString(R.string.notification_channel_general_title),
NotificationManager.IMPORTANCE_DEFAULT
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = application.getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.createNotificationChannel(normalChannel)
if (normal) {
// Create the NORMAL channel (used for likes, comments, replies, etc.)
val normalChannel = NotificationChannel(
application.getString(R.string.notification_channel_normal_id),
application.getString(R.string.notification_channel_general_title),
NotificationManager.IMPORTANCE_DEFAULT
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this

// Create the IMPORTANT channel (used for 2fa auth, for example)
val importantChannel = NotificationChannel(
application.getString(R.string.notification_channel_important_id),
application.getString(R.string.notification_channel_important_title),
NotificationManager.IMPORTANCE_HIGH
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(importantChannel)

// Create the REMINDER channel (used for various reminders, like Quick Start, etc.)
val reminderChannel = NotificationChannel(
application.getString(R.string.notification_channel_reminder_id),
application.getString(R.string.notification_channel_reminder_title),
NotificationManager.IMPORTANCE_LOW
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(reminderChannel)

// Create the TRANSIENT channel (used for short-lived notifications such as processing a Like/Approve,
// or media upload)
val transientChannel = NotificationChannel(
application.getString(R.string.notification_channel_transient_id),
application.getString(R.string.notification_channel_transient_title),
NotificationManager.IMPORTANCE_DEFAULT
)
transientChannel.setSound(null, null)
transientChannel.enableVibration(false)
transientChannel.enableLights(false)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(transientChannel)

// Create the WEEKLY ROUNDUP channel (used for weekly roundup notification containing weekly stats)
val weeklyRoundupChannel = NotificationChannel(
application.getString(R.string.notification_channel_weekly_roundup_id),
application.getString(R.string.notification_channel_weekly_roundup_title),
NotificationManager.IMPORTANCE_LOW
)
// Register the channel with the system; you can't change the importance or other notification behaviors
// after this
notificationManager.createNotificationChannel(weeklyRoundupChannel)
notificationManager.createNotificationChannel(normalChannel)
}
if (important) {
// Create the IMPORTANT channel (used for 2fa auth, for example)
val importantChannel = NotificationChannel(
application.getString(R.string.notification_channel_important_id),
application.getString(R.string.notification_channel_important_title),
NotificationManager.IMPORTANCE_HIGH
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(importantChannel)
}
if (reminder) {
// Create the REMINDER channel (used for various reminders, like Quick Start, etc.)
val reminderChannel = NotificationChannel(
application.getString(R.string.notification_channel_reminder_id),
application.getString(R.string.notification_channel_reminder_title),
NotificationManager.IMPORTANCE_LOW
)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(reminderChannel)
}
if (transient) {
// Create the TRANSIENT channel (used for short-lived notifications such as processing a Like/Approve,
// or media upload)
val transientChannel = NotificationChannel(
application.getString(R.string.notification_channel_transient_id),
application.getString(R.string.notification_channel_transient_title),
NotificationManager.IMPORTANCE_DEFAULT
)
transientChannel.setSound(null, null)
transientChannel.enableVibration(false)
transientChannel.enableLights(false)
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(transientChannel)
}
if (weeklyRoundup) {
// Create the WEEKLY ROUNDUP channel (used for weekly roundup notification containing weekly stats)
val weeklyRoundupChannel = NotificationChannel(
application.getString(R.string.notification_channel_weekly_roundup_id),
application.getString(R.string.notification_channel_weekly_roundup_title),
NotificationManager.IMPORTANCE_LOW
)
// Register the channel with the system; you can't change the importance or other notification behaviors
// after this
notificationManager.createNotificationChannel(weeklyRoundupChannel)
}
}
}

@@ -980,10 +993,19 @@ class AppInitializer @Inject constructor(
}

private fun updateNotificationSettings() {
if(!jetpackFeatureRemovalPhaseHelper.shouldShowNotifications())
if (!jetpackFeatureRemovalPhaseHelper.shouldShowNotifications()) {
NotificationsUtils.cancelAllNotifications(application)
else
// Only create the transient notification channel to handle upload notifications
createNotificationChannelsOnSdk26(
normal = false,
important = false,
reminder = false,
transient = true,
weeklyRoundup = false,
)
} else {
createNotificationChannelsOnSdk26()
}
}

companion object {

0 comments on commit db61f3b

Please sign in to comment.