Код: Выделить всё
@WorkerThread
suspend fun showNotification(
message: NotificationData,
notificationJson: String,
fromUser: Boolean,
isShowingOption: Boolean = true,
update: Boolean = false,
) {
val notificationId = System.currentTimeMillis().toInt() + Random.nextInt(1, 101)
setUpNotificationChannels()
updateShortcuts(message)
val contactName = "${message.firstName} ${message.lastName}"
val shortcutId = message.id.toString()
val bitmap = message.userImage?.let { getBitmapFromUrl(it) }
val user = Person.Builder().setName(context.getString(R.string.sender_you)).build()
val person = Person.Builder().setName(contactName).build()
val messagingStyle = NotificationCompat.MessagingStyle(user)
val notificationMessage = NotificationCompat.MessagingStyle.Message(
message.message, System.currentTimeMillis(), person
)
messagingStyle.addMessage(notificationMessage)
val conversationId = message.initiator.hashCode()
val groupKeyForPerson = "group_key_person_${message.initiator}"
if (preferenceDataStoreHelper == null) {
preferenceDataStoreHelper = PreferenceDataStoreHelper(Currently.instance)
}
var resultPendingIntent: PendingIntent? = null
val userData = preferenceDataStoreHelper?.getProfileData(
PreferenceDataStoreConstants.USER_PROFILE_DATA, ""
).toString()
if (userData.isNotEmpty()) {
val userUnderData = Gson().fromJson(userData, UserData::class.java)
val completedSteps = userUnderData?.actions?.completedSteps
if (completedSteps != null) {
val resultIntent = if (completedSteps >= 1) {
Intent(
context, HomeActivity::class.java
).apply {
putExtra(PUSH_NOTIFICATION_DATA, notificationJson)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
} else {
Intent(
context, SignUpActivity::class.java
).apply {
putExtra(PUSH_NOTIFICATION_DATA, notificationJson)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
}
resultPendingIntent = PendingIntent.getActivity(
context, notificationId, resultIntent, FLAG_MUTABLE
)
}
}
val builder = NotificationCompat.Builder(context, CHANNEL_NEW_MESSAGES)
.setSmallIcon(R.drawable.push_notif_icon)
.setContentTitle(contactName)
.setCategory(Notification.CATEGORY_MESSAGE)
.setLargeIcon(bitmap)
.setShortcutId(shortcutId) // Required for bubbles
.setLocusId(LocusIdCompat(shortcutId))
.addPerson(person)
.setShowWhen(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(resultPendingIntent)
.setStyle(messagingStyle)
.setGroup(groupKeyForPerson) //Group notifications
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
if (update) {
builder.setOnlyAlertOnce(true)
}
//Post the individual bubble notification
notificationManager.notify(notificationId, builder.build())
//Post group summary if multiple active
val activeNotifications = notificationManager.activeNotifications?.filter {
it.notification.group == groupKeyForPerson
}
if (activeNotifications != null && activeNotifications.size > 1) {
val summaryBuilder = NotificationCompat.Builder(context, CHANNEL_NEW_MESSAGES)
.setContentTitle("$contactName (${activeNotifications.size} new messages)")
.setSmallIcon(R.drawable.push_notif_icon)
.setStyle(NotificationCompat.InboxStyle().setSummaryText(contactName))
.setGroup(groupKeyForPerson)
.setGroupSummary(true)
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
.setAutoCancel(true)
notificationManager.notify(conversationId, summaryBuilder.build())
} else {
notificationManager.cancel(groupKeyForPerson.hashCode())
}
//this conversation exists and can bubble.
val shortcutIntent = Intent(context, HomeActivity::class.java).apply {
action = Intent.ACTION_VIEW
putExtra(PUSH_NOTIFICATION_DATA, notificationJson)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
}
val shortcut = ShortcutInfoCompat.Builder(context, groupKeyForPerson)
.setShortLabel(contactName)
.setLongLabel(contactName)
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_new_message))
.setIntent(shortcutIntent)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}
Сообщения появляются в виде отдельных уведомлений, а не сгруппированных в одном. /> Уведомления о пузырях не агрегируют несколько сообщений в один пузырь. /> < /li>
Уведомление об резюме группы должно показать подсчет новых сообщений и действовать в качестве краткого изложения разговора. < /p>
< /li>
< /ul>
Подробнее здесь: https://stackoverflow.com/questions/797 ... e-whatsapp