|
|
@ -24,10 +24,10 @@ import android.app.PendingIntent
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
|
|
import android.os.Build.VERSION
|
|
|
|
import android.os.Build.VERSION
|
|
|
|
import android.os.Build.VERSION_CODES
|
|
|
|
import android.os.Build.VERSION_CODES
|
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
|
import androidx.core.app.ActivityCompat.checkSelfPermission
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.app.NotificationCompat.InboxStyle
|
|
|
|
import androidx.core.app.NotificationCompat.InboxStyle
|
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
@ -57,30 +57,24 @@ internal class SystemTrayNotifier @Inject constructor(
|
|
|
|
override fun postNewsNotifications(
|
|
|
|
override fun postNewsNotifications(
|
|
|
|
newsResources: List<NewsResource>,
|
|
|
|
newsResources: List<NewsResource>,
|
|
|
|
) = with(context) {
|
|
|
|
) = with(context) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
|
if (checkSelfPermission(this, permission.POST_NOTIFICATIONS) != PERMISSION_GRANTED) {
|
|
|
|
this,
|
|
|
|
|
|
|
|
permission.POST_NOTIFICATIONS,
|
|
|
|
|
|
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val truncatedNewsResources = newsResources
|
|
|
|
val truncatedNewsResources = newsResources.take(MAX_NUM_NOTIFICATIONS)
|
|
|
|
.take(MAX_NUM_NOTIFICATIONS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val newsNotifications = truncatedNewsResources
|
|
|
|
val newsNotifications = truncatedNewsResources.map { newsResource ->
|
|
|
|
.map { newsResource ->
|
|
|
|
createNewsNotification {
|
|
|
|
createNewsNotification {
|
|
|
|
setSmallIcon(
|
|
|
|
setSmallIcon(
|
|
|
|
com.google.samples.apps.nowinandroid.core.common.R.drawable.core_common_ic_nia_notification,
|
|
|
|
com.google.samples.apps.nowinandroid.core.common.R.drawable.core_common_ic_nia_notification,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.setContentTitle(newsResource.title)
|
|
|
|
.setContentTitle(newsResource.title)
|
|
|
|
.setContentText(newsResource.content)
|
|
|
|
.setContentText(newsResource.content)
|
|
|
|
.setContentIntent(newsPendingIntent(newsResource))
|
|
|
|
.setContentIntent(newsPendingIntent(newsResource))
|
|
|
|
.setGroup(NEWS_NOTIFICATION_GROUP)
|
|
|
|
.setGroup(NEWS_NOTIFICATION_GROUP)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
val summaryNotification = createNewsNotification {
|
|
|
|
val summaryNotification = createNewsNotification {
|
|
|
|
val title = getString(
|
|
|
|
val title = getString(
|
|
|
|
R.string.core_notifications_news_notification_group_summary,
|
|
|
|
R.string.core_notifications_news_notification_group_summary,
|
|
|
@ -117,9 +111,7 @@ internal class SystemTrayNotifier @Inject constructor(
|
|
|
|
newsResources: List<NewsResource>,
|
|
|
|
newsResources: List<NewsResource>,
|
|
|
|
title: String,
|
|
|
|
title: String,
|
|
|
|
): InboxStyle = newsResources
|
|
|
|
): InboxStyle = newsResources
|
|
|
|
.fold(InboxStyle()) { inboxStyle, newsResource ->
|
|
|
|
.fold(InboxStyle()) { inboxStyle, newsResource -> inboxStyle.addLine(newsResource.title) }
|
|
|
|
inboxStyle.addLine(newsResource.title)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.setBigContentTitle(title)
|
|
|
|
.setBigContentTitle(title)
|
|
|
|
.setSummaryText(title)
|
|
|
|
.setSummaryText(title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|