Created
November 6, 2017 08:49
-
-
Save chetdeva/8c04f05294a949fc3c4a3577cd12d113 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Reusable | |
class AppPushNotification @Inject | |
constructor(private val notificationManager: NotificationManager, | |
private val resolver: NotificationItemResolver, | |
private val notificationBuilder: NotificationBuilder, | |
private val collapsingNotificationManager: CollapsingNotificationManager) : PushNotification { | |
override fun push(context: Context, data: Map<String, String>) { | |
val item = resolver.resolve(context, data, collapsingNotificationManager.getNotificationsToCollapse(data)) | |
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { | |
createChannel(context, item.channel()) | |
} | |
if (item.shouldCollapse()) { | |
item.notificationsIdsToCollapse()?.let { cancelAll(it) } | |
} | |
notify(item.id(), notificationBuilder.build(context, item)) | |
} | |
override fun notify(notificationId: Int, notification: Notification) { | |
notificationManager.notify(notificationId, notification) | |
} | |
override fun cancel(notificationId: Int) { | |
notificationManager.cancel(notificationId) | |
} | |
private fun cancelAll(notificationIds: List<Int>) { | |
for (notificationId in notificationIds) { | |
cancel(notificationId) | |
} | |
} | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
override fun createChannel(context: Context, channel: AppNotificationChannel) { | |
val channelTitle = context.getString(channel.titleRes) | |
val importance = NotificationManager.IMPORTANCE_DEFAULT | |
val notificationChannel = NotificationChannel(channel.channelId, channelTitle, importance) | |
notificationChannel.setShowBadge(true) | |
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC | |
notificationManager.createNotificationChannel(notificationChannel) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment