// 示例代码
class SimpleNotifier : BaseNotifier() {
override fun generateNotifyId(content: NotificationContent): Int {
// always 1
return 1
}
override fun createNotificationInstance(
context: Context,
builder: NotificationCompat.Builder,
content: NotificationContent
): Notification {
return builder.setContentTitle("测试通知消息")
.setContentText("消息内容")
.setNumber(1)
.setAutoCancel(true)
.setSmallIcon(android.R.drawable.ic_notification_clear_all)
// .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setContentIntent(NotificationUtil.generateOpenAppIntent(context))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build()
}
@RequiresApi(Build.VERSION_CODES.O)
override fun createNotificationChannel(
context: Context,
content: NotificationContent
): NotificationChannel? {
// NotificationChannels are required for Notifications on O (API 26) and above.
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Initializes NotificationChannel.
val notificationChannel = NotificationChannel(config.id, config.name, config.importance)
notificationChannel.description = config.description
notificationChannel.enableVibration(config.enableVibration)
notificationChannel.lockscreenVisibility = config.lockscreenVisibility
notificationChannel.setSound(getCustomSoundUri(context), Notification.AUDIO_ATTRIBUTES_DEFAULT)
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
notificationChannel
} else null
}
}
Notifications.setNotifier(SimpleNotifier())
Notifications.notify(this, NotificationContent("标题", "内容", mapOf()))