Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyAndroidFirebaseMessagingService : FirebaseMessagingService(){
- private val TAG = "MyAndroidFCMService"
- internal var bitmap: Bitmap? = null
- private var title: String? = null
- private var body: String? = null
- private val summary: String? = null
- private var summaryBig: String? = null
- private var bigText: String? = null
- override fun onMessageReceived(remoteMessage: RemoteMessage?) {
- //Log data to Log Cat
- Log.d(TAG, "From: " + remoteMessage!!.from!!)
- // Log.d(TAG, "Notification Message Body: " + remoteMessage.notification!!.body!!)
- //create notification
- Log.d("BACKFCM", "From: " + remoteMessage!!.data.toString())
- title = remoteMessage.data["title"]
- body = remoteMessage.data["body"]
- summaryBig = remoteMessage.data["bigSummary"]
- bigText = remoteMessage.data["bigText"]
- Log.d("BACKFCM", "title " + remoteMessage.data["title"])
- Log.d("BACKFCM", "Pesan " + remoteMessage.data["body"])
- // Log.d("BACKFCM", "bigSummary " + remoteMessage.data["bigSummary"])
- // Log.d("BACKFCM", "bigText " + remoteMessage.data["bigText"])
- // createNotification(remoteMessage.notification!!.body)
- if (isBackgroundRunning(applicationContext)) {
- sendNotification(title, body, summaryBig, bigText)
- } else {
- val intentKill = Intent(applicationContext.getString(R.string.broadcastIntentFilter))
- applicationContext.sendBroadcast(intentKill)
- }
- }
- private fun sendNotification(title: String?, body: String?, summaryBig: String?, bigText: String?) {
- val intent = Intent(this, SplashScreenActivity::class.java)
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
- PendingIntent.FLAG_ONE_SHOT)
- val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
- val builder = NotificationCompat.Builder(this)
- builder.setStyle(NotificationCompat.BigTextStyle(builder)
- .bigText(bigText)
- .setBigContentTitle(title)
- .setSummaryText(summaryBig))
- .setContentTitle(title)
- .setContentText(body)
- .setSound(defaultSoundUri)
- .setAutoCancel(true)
- .setSmallIcon(R.mipmap.icon)
- .setPriority(Notification.PRIORITY_MAX)
- .setContentIntent(pendingIntent)
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- notificationManager.notify(0, builder.build())
- }
- fun getBitmapfromUrl(imageUrl: String): Bitmap? {
- try {
- val url = URL(imageUrl)
- val connection = url.openConnection() as HttpURLConnection
- connection.doInput = true
- connection.connect()
- val input = connection.inputStream
- return BitmapFactory.decodeStream(input)
- } catch (e: Exception) {
- // TODO Auto-generated catch block
- e.printStackTrace()
- return null
- }
- }
- fun isBackgroundRunning(context: Context): Boolean {
- val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
- val runningProcesses = am.runningAppProcesses
- for (processInfo in runningProcesses) {
- if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- for (activeProcess in processInfo.pkgList) {
- if (activeProcess == context.packageName) {
- //If your app is the process in foreground, then it's not in running in background
- return false
- }
- }
- }
- }
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement