@ -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 @@ 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 . ic _nia _notification ,
com . google . samples . apps . nowinandroid . core . common . R . drawable . 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 . news _notification _group _summary ,
R . string . news _notification _group _summary ,
@ -117,9 +111,7 @@ 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 )
}
}