notification in android studio kotlin
https://ift.tt/38ihoaY
Notification in android studio kotlin
private val notificationManager: NotificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } private val notificationIntent by lazy { PendingIntent.getService(this, 0, Intent(this, CallServices::class.java).apply { action = ACTION_MAIN }, 0) } private val closeIntent by lazy { PendingIntent.getService(this, 0, Intent(this, CallServices::class.java).apply { action = NOTIFICATION_ACTION_CALL_CLOSE }, 0) } private val declineIntent by lazy { PendingIntent.getService(this, 0, Intent(this, CallServices::class.java).apply { action = NOTIFICATION_ACTION_CALL_DECLINE }, 0) } private val acceptIntent by lazy { PendingIntent.getService(this, 0, Intent(this, CallServices::class.java).apply { action = NOTIFICATION_ACTION_CALL_ACCEPT }, 0) } internal fun showNotification(type: NotificationType) { LogUtils.e(CommonTAG, "CN->CallServices FN--> showNotification() type--> $type") val notification: Notification = when (type) { NotificationType.STARTUP -> { getStartupNotification(this, callType, usageType, this.notificationIntent, acceptIntent, declineIntent, this.closeIntent)!! } NotificationType.ONGOING -> { val builder = CallsNotificationUtils.getBaseBuilder(this, notificationIntent, callType, usageType) with(builder) { this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_hangup), closeIntent).build()) } builder.setOngoing(true).build() } NotificationType.MISSED_CALL -> { val appIntent = Intent(this, if ((this.applicationContext as MyApp).foregroundManager.isForeground) CallActivity::class.java else SplashActivity::class.java) appIntent.action = Intent.ACTION_MAIN val appPending = PendingIntent.getService(this, 0, appIntent, 0) val builder = CallsNotificationUtils.getBaseBuilder(this, appPending, callType, usageType, autoCancel = true, ongoing = false, body = getString(R.string.calls_missed_from, showedName)) builder.setAutoCancel(true).build() } NotificationType.ACCEPT_CALL -> { //speaker pending Intent val speakerIntent = Intent(this, CallServices::class.java) speakerIntent.action = NOTIFICATION_ACTION_CALL_SPEAKER val speakerPending = PendingIntent.getService(this, 0, speakerIntent, 0) //mute pending Intent val muteIntent = Intent(this, CallServices::class.java) muteIntent.action = NOTIFICATION_ACTION_CALL_MUTE val mutePending = PendingIntent.getService(this, 0, muteIntent, 0) val builder = CallsNotificationUtils.getBaseBuilder(this, notificationIntent, callType, usageType) with(builder) { // adds usual "Hang up" action this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_hangup), closeIntent).build()) // adds "Toggle speaker" action if (!isSpeaker) this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_speaker), speakerPending).build()) else this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_earpiece), speakerPending).build()) // adds "Toggle mute" action if (!isMute) this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_mute), mutePending).build()) else this.addAction(NotificationCompat.Action.Builder(0, getString(R.string.action_calls_un_mute), mutePending).build()) } callStatus = VoiceVideoStatus.ACCEPT builder.build() } } notificationManager.notify(SERVICE_NOTIFICATION_ID, notification) } fun getBaseBuilder(context: Context, primaryPendingIntent: PendingIntent, callType: VoiceVideoCallType, usageType: VoiceVideoUsageType, ongoing: Boolean = true, autoCancel: Boolean = false, title: String? = null, body: String? = null): NotificationCompat.Builder { val channelID = context.getString(R.string.notification_channel_id_calls) if (hasOreo()) { val callInviteChannel = NotificationChannel(channelID, context.getString(R.string.notif_channel_calls_description), NotificationManager.IMPORTANCE_HIGH) callInviteChannel.description = context.getString(R.string.notif_channel_calls_description) callInviteChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE callInviteChannel.enableVibration(false) callInviteChannel.enableLights(true) callInviteChannel.lightColor = Color.RED (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(callInviteChannel) } return NotificationCompat.Builder(context, channelID).setSmallIcon(R.drawable.ic_notification_tray).setContentTitle(if (!title.isNullOrBlank()) title else { context.getString(R.string.call_notif_title, context.getString(R.string.app_name)) }).setContentText(if (!body.isNullOrBlank()) body else { context.getString(if (usageType == VoiceVideoUsageType.OUTGOING) { if (callType == VoiceVideoCallType.VOICE) R.string.call_notif_body_out_vc else R.string.call_notif_body_out_vd } else { if (callType == VoiceVideoCallType.VOICE) R.string.call_notif_body_inc_vc else R.string.call_notif_body_inc_vd }) }).setPriority(NotificationCompat.PRIORITY_MAX).setAutoCancel(autoCancel).setOngoing(ongoing).setTicker(context.getString(R.string.call_notif_title, context.getString(R.string.app_name))).setContentIntent(primaryPendingIntent).setOnlyAlertOnce(true).setCategory(NotificationCompat.CATEGORY_CALL).setColor(ContextCompat.getColor(context, R.color.colorAccent)) }
via Blogger https://ift.tt/2R3QSMI












