-
Notifications
You must be signed in to change notification settings - Fork 3
/
AppPushNotification.kt
65 lines (56 loc) · 2.54 KB
/
AppPushNotification.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.fueled.collapsingnotifications.notification
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import com.fueled.collapsingnotifications.core.NotificationBuilder
import com.fueled.collapsingnotifications.core.NotificationItemResolver
import com.fueled.collapsingnotifications.core.PushNotification
import dagger.Reusable
import javax.inject.Inject
/**
* Copyright (c) 2017 Fueled. All rights reserved.
* PushNotification interface provides methods for push(), notify() and cancel() notifications.
* createChannel() is important for Android O and above.
*
* @author chetansachdeva on 04/09/17
*/
@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)
}
}