Skip to content

Instantly share code, notes, and snippets.

@firecatmusic
Last active March 6, 2022 18:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save firecatmusic/b97d9cb838ce01c41093acf8540a7b5b to your computer and use it in GitHub Desktop.
Save firecatmusic/b97d9cb838ce01c41093acf8540a7b5b to your computer and use it in GitHub Desktop.
class FCMService2 : FirebaseMessagingService() {
private var pendingIntent: PendingIntent? = null
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.e("get_type_data", remoteMessage!!.data.toString())
createNotification(remoteMessage.data)
}
private fun createNotification(messageBody: Map<String, String>) {
val content: String? = messageBody["content"]
val id: String? = messageBody["id_orders"]
val type: String? = messageBody["type"]
var foregroud = false
try {
foregroud = ForegroundCheckTask().execute(this).get()
} catch (e: InterruptedException) {
e.printStackTrace()
} catch (e: ExecutionException) {
e.printStackTrace()
}
Log.e("foreground", foregroud.toString())
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val intent: Intent
val random = Random().nextInt(1000).toString()
if (type == "5") {
if (foregroud) {
// intent = Intent("NEW_ORDER")
// intent.putExtra(Cons.PREF_ORDER_ID, id)
// sendBroadcast(intent)
intent = Intent(this, OrderBaruActivity::class.java)
intent.putExtra(Cons.PREF_ORDER_ID, id)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
startActivity(intent)
} else {
intent = Intent(this, OrderBaruActivity::class.java)
intent.putExtra(Cons.PREF_ORDER_ID, id)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} else if (type == "6") {
if (foregroud) {
intent = Intent("REFRESH_WALLET")
intent.putExtra("refreshData", 1)
intent.putExtra("message", content.toString())
sendBroadcast(intent)
} else {
intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} else if (type == "9") {
if (foregroud) {
intent = Intent(this, ChatAdminActivity::class.java)
intent.putExtra("intent_backchat", 1)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} else {
intent = Intent(this, ChatAdminActivity::class.java)
intent.putExtra("intent_backchat", 1)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} else if (type == "11") {
if (foregroud) {
intent = Intent(this, PopUpTercancel::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
startActivity(intent)
} else {
intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} else if (type == "12") {
if (foregroud) {
intent = Intent("REFRESH_WALLET")
intent.putExtra("refreshData", 1)
intent.putExtra("message",content.toString())
sendBroadcast(intent)
} else {
intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
} else if (type == "13") {
if (foregroud) {
intent = Intent("STATUS_SALDO")
intent.putExtra("showWarn", 1)
intent.putExtra("message", content.toString())
sendBroadcast(intent)
} else {
intent = Intent(this, MainActivity::class.java)
intent.putExtra("showWarn", 1)
intent.putExtra("message", content.toString())
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(random) /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
if (id != null) {
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, id)
.setSmallIcon(R.mipmap.ic_launcher_circle_fivestar)
.setContentTitle(this.resources.getString(R.string.app_name))
.setContentText(content)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
channel.description = applicationContext.getString(R.string.app_name)
channel.setShowBadge(true)
channel.canShowBadge()
channel.enableLights(true)
channel.lightColor = applicationContext.getColor(R.color.colorPrimaryDark)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Integer.parseInt(id) /* ID of notification */, notificationBuilder.build())
} else {
Log.e("randomidforNotif", random)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, random)
.setSmallIcon(R.mipmap.ic_launcher_circle_fivestar)
.setContentTitle(this.resources.getString(R.string.app_name))
.setContentText(content)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(random, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
channel.description = applicationContext.getString(R.string.app_name)
channel.setShowBadge(true)
channel.canShowBadge()
channel.enableLights(true)
channel.lightColor = applicationContext.getColor(R.color.colorPrimaryDark)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Integer.parseInt(random) /* ID of notification */, notificationBuilder.build())
}
}
@SuppressLint("StaticFieldLeak")
private inner class ForegroundCheckTask : AsyncTask<Context, Void, Boolean>() {
override fun doInBackground(vararg params: Context): Boolean? {
val context = params[0].applicationContext
return isAppOnForeground(context)
}
private fun isAppOnForeground(context: Context): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val appProcesses = activityManager.runningAppProcesses ?: return false
val packageName = context.packageName
for (appProcess in appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName == packageName) {
return true
}
}
return false
}
}
override fun onNewToken(token: String) {
super.onNewToken(token)
val dbs = DBS.with(applicationContext)
dbs.getfcmId = token
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment