Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun createNotification(messageTitle: String, from: MutableMap<String, String>?){
- val title: String
- val body : String
- val name = from?.get(KEY_SENDER_NAME) ?: ""
- val bundle = Bundle()
- val notificationIntent = Intent(this, AppActivity::class.java).apply {
- flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
- }
- bundle.putBoolean(BUNDLE_FROM_NOTIFICATION, true)
- when(messageTitle){
- NotificationType.NEW_MATCH.type -> {
- title = getString(R.string.match_dialog_new_match_title_notification)
- body = getString(R.string.match_dialog_with_prefix_notification, name)
- val matchId = from?.get(KEY_MATCH_ID)?.toIntOrNull() ?: 0
- bundle.putString(BUNDLE_NOTIFICATION_TYPE, BUNDLE_NEW_MATCH)
- bundle.putInt(BUNDLE_MATCH_ID, matchId)
- }
- NotificationType.NEW_MESSAGE.type -> {
- val chatId = from?.get(KEY_CHAT_ID)?.toIntOrNull() ?: 0
- title = getString(R.string.message_dialog_message_new_message)
- body = getString(R.string.message_dialog_message_from_notification, name)
- bundle.putString(BUNDLE_NOTIFICATION_TYPE, BUNDLE_NEW_MESSAGE)
- bundle.putInt(BUNDLE_CHAT_ID, chatId)
- }
- else -> return
- }
- notificationIntent.putExtras(bundle)
- val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
- val notificationId = Random().nextInt()
- val intent = PendingIntent.getActivity(this, notificationId, notificationIntent, FLAG_ONE_SHOT)
- val mBuilder = NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
- .setSmallIcon(R.drawable.ic_logo)
- .setContentTitle(title)
- .setContentText(body)
- .setAutoCancel(true)
- .setGroup(notificationId.toString())
- .setPriority(NotificationCompat.PRIORITY_HIGH)
- .setSound(alarmSound)
- .setContentIntent(intent)
- val mNotificationManager =
- getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- mNotificationManager.notify(notificationId, mBuilder.build())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement