Skip to content

Commit

Permalink
Implement dismiss action for blogging prompts onboarding notification
Browse files Browse the repository at this point in the history
  • Loading branch information
RenanLukas committed Apr 22, 2022
1 parent af7b759 commit 7c0348e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1374,12 +1374,14 @@ public static Intent createMainActivityAndSiteCreationActivityIntent(Context con
@NonNull
public static Intent createMainActivityAndShowBloggingPromptsOnboardingActivityIntent(
final Context context,
@Nullable final NotificationType notificationType
@Nullable final NotificationType notificationType,
final int notificationId
) {
final Intent intent = new Intent(context, WPMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra(WPMainActivity.ARG_BLOGGING_PROMPTS_ONBOARDING, true);
intent.putExtra(WPMainActivity.ARG_DISMISS_NOTIFICATION, notificationId);
if (notificationType != null) {
intent.putExtra(ARG_NOTIFICATION_TYPE, notificationType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ class BloggingPromptsOnboardingNotificationHandler @Inject constructor(
return accountStore.hasAccessToken()
}

override fun buildIntent(context: Context): Intent {
override fun buildFirstActionIntent(context: Context, notificationId: Int): Intent {
return ActivityLauncher.createMainActivityAndShowBloggingPromptsOnboardingActivityIntent(
context, BLOGGING_PROMPTS_ONBOARDING
context, BLOGGING_PROMPTS_ONBOARDING, notificationId
)
}

override fun buildSecondActionIntent(context: Context, notificationId: Int): Intent? {
return ActivityLauncher.createMainActivityDismissNotificationIntent(context, notificationId)
}

override fun onNotificationShown() {
notificationsTracker.trackShownNotification(BLOGGING_PROMPTS_ONBOARDING)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class BloggingPromptsOnboardingNotificationScheduler @Inject constructor(
title = R.string.blogging_prompts_onboarding_notification_title,
text = R.string.blogging_prompts_onboarding_notification_text,
icon = R.drawable.ic_wordpress_white_24dp,
actionIcon = -1,
actionTitle = R.string.blogging_prompts_onboarding_notification_action
firstActionIcon = -1,
firstActionTitle = R.string.blogging_prompts_onboarding_notification_action,
secondActionIcon = -1,
secondActionTitle = R.string.blogging_prompts_notification_dismiss
)
localNotificationScheduler.scheduleOneTimeNotification(firstNotification)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CreateSiteNotificationHandler @Inject constructor(
return accountStore.hasAccessToken() && !siteStore.hasSite()
}

override fun buildIntent(context: Context): Intent {
override fun buildFirstActionIntent(context: Context, notificationId: Int): Intent {
return ActivityLauncher.createMainActivityAndSiteCreationActivityIntent(context, CREATE_SITE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CreateSiteNotificationScheduler
title = R.string.create_site_notification_title,
text = R.string.create_site_notification_text,
icon = R.drawable.ic_wordpress_white_24dp,
actionIcon = -1,
actionTitle = R.string.create_site_notification_create_site_action
firstActionIcon = -1,
firstActionTitle = R.string.create_site_notification_create_site_action
)
val secondNotification = firstNotification.copy(
delay = 8 // 1 week after first notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ data class LocalNotification(
@StringRes val title: Int,
@StringRes val text: Int,
@DrawableRes val icon: Int,
@DrawableRes val actionIcon: Int,
@StringRes val actionTitle: Int,
@DrawableRes val firstActionIcon: Int,
@StringRes val firstActionTitle: Int,
@DrawableRes val secondActionIcon: Int? = null,
@StringRes val secondActionTitle: Int? = null,
val uniqueId: Int? = null
) {
val id = uniqueId ?: NotificationPushIds.LOCAL_NOTIFICATION_ID + type.ordinal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class LocalNotificationHandlerFactory @Inject constructor(

interface LocalNotificationHandler {
fun shouldShowNotification(): Boolean
fun buildIntent(context: Context): Intent
fun buildFirstActionIntent(context: Context, notificationId: Int): Intent
fun buildSecondActionIntent(context: Context, notificationId: Int): Intent? {
return null
}
fun onNotificationShown()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,33 @@ class LocalNotificationWorker(

val localNotificationHandler = localNotificationHandlerFactory.buildLocalNotificationHandler(type)
if (localNotificationHandler.shouldShowNotification()) {
NotificationManagerCompat.from(context).notify(id, localNotificationBuilder().build())
NotificationManagerCompat.from(context).notify(id, localNotificationBuilder(id).build())
localNotificationHandler.onNotificationShown()
}

return Result.success()
}

private fun localNotificationBuilder(): NotificationCompat.Builder {
private fun localNotificationBuilder(notificationId: Int): NotificationCompat.Builder {
val title = inputData.getInt(TITLE, -1)
val text = inputData.getInt(TEXT, -1)
val icon = inputData.getInt(ICON, -1)
val actionIcon = inputData.getInt(ACTION_ICON, -1)
val actionTitle = inputData.getInt(ACTION_TITLE, -1)
val firstActionIcon = inputData.getInt(FIRST_ACTION_ICON, -1)
val firstActionTitle = inputData.getInt(FIRST_ACTION_TITLE, -1)
val secondActionIcon = inputData.getInt(SECOND_ACTION_ICON, -1)
val secondActionTitle = inputData.getInt(SECOND_ACTION_TITLE, -1)

return NotificationCompat.Builder(context, context.getString(R.string.notification_channel_normal_id)).apply {
val pendingIntent = getPendingIntent()
setContentIntent(pendingIntent)
val firstActionPendingIntent = getFirstActionPendingIntent(notificationId)
setContentIntent(firstActionPendingIntent)
setSmallIcon(icon)
setContentTitle(context.getString(title))
setContentText(context.getString(text))
addAction(actionIcon, context.getString(actionTitle), pendingIntent)
addAction(firstActionIcon, context.getString(firstActionTitle), firstActionPendingIntent)
val secondActionPendingIntent = getSecondActionPendingIntent(notificationId)
if (secondActionTitle != -1) {
addAction(secondActionIcon, context.getString(secondActionTitle), secondActionPendingIntent)
}
priority = NotificationCompat.PRIORITY_DEFAULT
setCategory(NotificationCompat.CATEGORY_REMINDER)
setAutoCancel(true)
Expand All @@ -56,13 +62,24 @@ class LocalNotificationWorker(
}
}

private fun getPendingIntent(): PendingIntent {
private fun getFirstActionPendingIntent(notificationId: Int): PendingIntent {
val type = Type.fromTag(inputData.getString(TYPE))
val handler = type?.let { localNotificationHandlerFactory.buildLocalNotificationHandler(it) }
return PendingIntent.getActivity(
context,
0,
handler?.buildFirstActionIntent(context, notificationId),
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

private fun getSecondActionPendingIntent(notificationId: Int): PendingIntent {
val type = Type.fromTag(inputData.getString(TYPE))
val handler = type?.let { localNotificationHandlerFactory.buildLocalNotificationHandler(it) }
return PendingIntent.getActivity(
context,
0,
handler?.buildIntent(context),
handler?.buildSecondActionIntent(context, notificationId),
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
Expand All @@ -89,8 +106,10 @@ class LocalNotificationWorker(
private const val TITLE = "key_title"
private const val TEXT = "key_text"
private const val ICON = "key_icon"
private const val ACTION_ICON = "key_action_icon"
private const val ACTION_TITLE = "key_action_title"
private const val FIRST_ACTION_ICON = "key_first_action_icon"
private const val FIRST_ACTION_TITLE = "key_first_action_title"
private const val SECOND_ACTION_ICON = "key_second_action_icon"
private const val SECOND_ACTION_TITLE = "key_second_action_title"

fun buildData(localNotification: LocalNotification): Data {
return workDataOf(
Expand All @@ -99,8 +118,10 @@ class LocalNotificationWorker(
TITLE to localNotification.title,
TEXT to localNotification.text,
ICON to localNotification.icon,
ACTION_ICON to localNotification.actionIcon,
ACTION_TITLE to localNotification.actionTitle
FIRST_ACTION_ICON to localNotification.firstActionIcon,
FIRST_ACTION_TITLE to localNotification.firstActionTitle,
SECOND_ACTION_ICON to localNotification.secondActionIcon,
SECOND_ACTION_TITLE to localNotification.secondActionTitle,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class BloggingPromptsOnboardingNotificationSchedulerTest {
title = R.string.blogging_prompts_onboarding_notification_title,
text = R.string.blogging_prompts_onboarding_notification_text,
icon = R.drawable.ic_wordpress_white_24dp,
actionIcon = -1,
actionTitle = R.string.blogging_prompts_onboarding_notification_action
firstActionIcon = -1,
firstActionTitle = R.string.blogging_prompts_onboarding_notification_action
)
classToTest.scheduleBloggingPromptsOnboardingNotificationIfNeeded()
verify(localnotificationScheduler).scheduleOneTimeNotification(expectedLocalNotification)
Expand Down

0 comments on commit 7c0348e

Please sign in to comment.