lam bui
07/14/2023, 5:02 AMKseniia Shumelchyk
07/14/2023, 8:37 AMlam bui
07/14/2023, 8:38 AMlam bui
07/14/2023, 8:39 AMclass MyFirebaseMessagingService : FirebaseMessagingService() {
private val notificationManager by lazy { NotificationManagerCompat.from(applicationContext) }
override fun onNewToken(token: String) {
super.onNewToken(token)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
sendNotification(message)
}
private fun sendNotification(message: RemoteMessage) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
val pendingIntent = PendingIntent.getActivity(
this, NOTIFICATION_ID/* Request code */, intent,
pendingIntentFlags
)
Log.e("AAAAAAAAAAAAA", "${message.notification?.title}---- ${message.notification?.body}")
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
Log.e("AAAAAAAAAAAAA", "GRANT PERMISSION NOTIFICATION")
return
}
notificationManager.notify(
NOTIFICATION_ID, createNotification(
message.notification?.title, message.notification?.body,
addBuilder = {
setContentIntent(
pendingIntent
)
}
)
)
}
private fun createNotificationChannelIfAboveAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
applicationContext.getString(R.string.notification_message_channel_id),
applicationContext.getString(R.string.notification_message_channel_name),
NotificationManager.IMPORTANCE_HIGH
).apply {
description = applicationContext.getString(
R.string.notification_message_channel_description
)
}.let { notificationManager.createNotificationChannel(it) }
}
}
private fun createNotification(
title: String?,
content: String?,
addBuilder: NotificationCompat.Builder.() -> NotificationCompat.Builder,
): Notification {
createNotificationChannelIfAboveAndroidO()
return NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.notification_message_channel_id)
).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_app_notification)
.setColor(applicationContext.resources.getColor(R.color.colorPrimary))
.setContentText(content)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.extend(
NotificationCompat.WearableExtender()
.setBridgeTag("tagOne")
)
.run(addBuilder)
.build()
}
companion object {
const val NOTIFICATION_ID = 100
}
}
lam bui
07/14/2023, 8:39 AMlam bui
07/17/2023, 8:17 AM