Advertisement
kiev_north

fcm

Dec 13th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. class MyAndroidFirebaseMessagingService : FirebaseMessagingService(){
  2. private val TAG = "MyAndroidFCMService"
  3. internal var bitmap: Bitmap? = null
  4. private var title: String? = null
  5. private var body: String? = null
  6. private val summary: String? = null
  7. private var summaryBig: String? = null
  8. private var bigText: String? = null
  9. override fun onMessageReceived(remoteMessage: RemoteMessage?) {
  10. //Log data to Log Cat
  11. Log.d(TAG, "From: " + remoteMessage!!.from!!)
  12. // Log.d(TAG, "Notification Message Body: " + remoteMessage.notification!!.body!!)
  13.  
  14. //create notification
  15. Log.d("BACKFCM", "From: " + remoteMessage!!.data.toString())
  16. title = remoteMessage.data["title"]
  17. body = remoteMessage.data["body"]
  18. summaryBig = remoteMessage.data["bigSummary"]
  19. bigText = remoteMessage.data["bigText"]
  20. Log.d("BACKFCM", "title " + remoteMessage.data["title"])
  21. Log.d("BACKFCM", "Pesan " + remoteMessage.data["body"])
  22. // Log.d("BACKFCM", "bigSummary " + remoteMessage.data["bigSummary"])
  23. // Log.d("BACKFCM", "bigText " + remoteMessage.data["bigText"])
  24.  
  25. // createNotification(remoteMessage.notification!!.body)
  26. if (isBackgroundRunning(applicationContext)) {
  27. sendNotification(title, body, summaryBig, bigText)
  28. } else {
  29. val intentKill = Intent(applicationContext.getString(R.string.broadcastIntentFilter))
  30. applicationContext.sendBroadcast(intentKill)
  31. }
  32. }
  33.  
  34.  
  35.  
  36. private fun sendNotification(title: String?, body: String?, summaryBig: String?, bigText: String?) {
  37. val intent = Intent(this, SplashScreenActivity::class.java)
  38. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  39. val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  40. PendingIntent.FLAG_ONE_SHOT)
  41.  
  42. val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  43. val builder = NotificationCompat.Builder(this)
  44. builder.setStyle(NotificationCompat.BigTextStyle(builder)
  45. .bigText(bigText)
  46. .setBigContentTitle(title)
  47. .setSummaryText(summaryBig))
  48. .setContentTitle(title)
  49. .setContentText(body)
  50. .setSound(defaultSoundUri)
  51. .setAutoCancel(true)
  52. .setSmallIcon(R.mipmap.icon)
  53. .setPriority(Notification.PRIORITY_MAX)
  54. .setContentIntent(pendingIntent)
  55.  
  56. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  57.  
  58. notificationManager.notify(0, builder.build())
  59. }
  60.  
  61. fun getBitmapfromUrl(imageUrl: String): Bitmap? {
  62. try {
  63. val url = URL(imageUrl)
  64. val connection = url.openConnection() as HttpURLConnection
  65. connection.doInput = true
  66. connection.connect()
  67. val input = connection.inputStream
  68. return BitmapFactory.decodeStream(input)
  69.  
  70. } catch (e: Exception) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace()
  73. return null
  74.  
  75. }
  76.  
  77. }
  78.  
  79. fun isBackgroundRunning(context: Context): Boolean {
  80. val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
  81. val runningProcesses = am.runningAppProcesses
  82. for (processInfo in runningProcesses) {
  83. if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  84. for (activeProcess in processInfo.pkgList) {
  85. if (activeProcess == context.packageName) {
  86. //If your app is the process in foreground, then it's not in running in background
  87. return false
  88. }
  89. }
  90. }
  91. }
  92. return true
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement