-
-
Save iFr0z/8984a4512b62f8685d6f3715e46d940d to your computer and use it in GitHub Desktop.
WorkManagerđź”—Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun sendNotification(id: Int) { | |
val intent = Intent(applicationContext, MainActivity::class.java) | |
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK | |
intent.putExtra(NOTIFICATION_ID, id) | |
val notificationManager = | |
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager | |
val bitmap = applicationContext.vectorToBitmap(R.drawable.ic_schedule_black_24dp) | |
val titleNotification = applicationContext.getString(R.string.notification_title) | |
val subtitleNotification = applicationContext.getString(R.string.notification_subtitle) | |
val pendingIntent = getActivity(applicationContext, 0, intent, 0) | |
val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL) | |
.setLargeIcon(bitmap).setSmallIcon(R.drawable.ic_schedule_white) | |
.setContentTitle(titleNotification).setContentText(subtitleNotification) | |
.setDefaults(DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true) | |
notification.priority = PRIORITY_MAX | |
if (SDK_INT >= O) { | |
notification.setChannelId(NOTIFICATION_CHANNEL) | |
val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION) | |
val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE) | |
.setContentType(CONTENT_TYPE_SONIFICATION).build() | |
val channel = | |
NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH) | |
channel.enableLights(true) | |
channel.lightColor = RED | |
channel.enableVibration(true) | |
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) | |
channel.setSound(ringtoneManager, audioAttributes) | |
notificationManager.createNotificationChannel(channel) | |
} | |
notificationManager.notify(id, notification.build()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment