Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. private fun createNotification(messageTitle: String, from: MutableMap<String, String>?){
  2. val title: String
  3. val body : String
  4. val name = from?.get(KEY_SENDER_NAME) ?: ""
  5. val bundle = Bundle()
  6.  
  7. val notificationIntent = Intent(this, AppActivity::class.java).apply {
  8. flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
  9. }
  10.  
  11. bundle.putBoolean(BUNDLE_FROM_NOTIFICATION, true)
  12.  
  13. when(messageTitle){
  14. NotificationType.NEW_MATCH.type -> {
  15. title = getString(R.string.match_dialog_new_match_title_notification)
  16. body = getString(R.string.match_dialog_with_prefix_notification, name)
  17. val matchId = from?.get(KEY_MATCH_ID)?.toIntOrNull() ?: 0
  18. bundle.putString(BUNDLE_NOTIFICATION_TYPE, BUNDLE_NEW_MATCH)
  19. bundle.putInt(BUNDLE_MATCH_ID, matchId)
  20. }
  21. NotificationType.NEW_MESSAGE.type -> {
  22. val chatId = from?.get(KEY_CHAT_ID)?.toIntOrNull() ?: 0
  23. title = getString(R.string.message_dialog_message_new_message)
  24. body = getString(R.string.message_dialog_message_from_notification, name)
  25. bundle.putString(BUNDLE_NOTIFICATION_TYPE, BUNDLE_NEW_MESSAGE)
  26. bundle.putInt(BUNDLE_CHAT_ID, chatId)
  27. }
  28. else -> return
  29. }
  30. notificationIntent.putExtras(bundle)
  31. val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  32. val notificationId = Random().nextInt()
  33. val intent = PendingIntent.getActivity(this, notificationId, notificationIntent, FLAG_ONE_SHOT)
  34.  
  35. val mBuilder = NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
  36. .setSmallIcon(R.drawable.ic_logo)
  37. .setContentTitle(title)
  38. .setContentText(body)
  39. .setAutoCancel(true)
  40. .setGroup(notificationId.toString())
  41. .setPriority(NotificationCompat.PRIORITY_HIGH)
  42. .setSound(alarmSound)
  43. .setContentIntent(intent)
  44.  
  45. val mNotificationManager =
  46. getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  47.  
  48. mNotificationManager.notify(notificationId, mBuilder.build())
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement