package kr.co.tadadang.user import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.media.RingtoneManager import android.os.Build import android.util.Log import androidx.core.app.NotificationCompat import androidx.work.OneTimeWorkRequest import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage class FCMService: FirebaseMessagingService() { // [START receive_message] override fun onMessageReceived(remoteMessage: RemoteMessage) { // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ Log.d(TAG, "From: ${remoteMessage.from}") // Check if message contains a data payload. if (remoteMessage.data.isNotEmpty()) { Log.d(TAG, "Message data payload: ${remoteMessage.data}") // Check if data needs to be processed by long running job if (needsToBeScheduled()) { // For long-running tasks (10 seconds or more) use WorkManager. scheduleJob() } else { // Handle message within 10 seconds handleNow() } } // Check if message contains a notification payload. remoteMessage.notification?.let { Log.d(TAG, "Message Notification Body: ${it.body}") it.body?.let { it1 -> sendNotification(it1) } } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. } // [END receive_message] private fun needsToBeScheduled() = true // [START on_new_token] /** * Called if the FCM registration token is updated. This may occur if the security of * the previous token had been compromised. Note that this is called when the * FCM registration token is initially generated so this is where you would retrieve the token. */ override fun onNewToken(token: String) { Log.d(TAG, "Refreshed token: $token") // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // FCM registration token to your app server. sendRegistrationToServer(token) } // [END on_new_token] private fun scheduleJob() { // [START dispatch_job] val work = OneTimeWorkRequest.Builder(MyWorker::class.java) .build() WorkManager.getInstance(this) .beginWith(work) .enqueue() // [END dispatch_job] } private fun handleNow() { Log.d(TAG, "Short lived task is done.") } private fun sendRegistrationToServer(token: String?) { // TODO: Implement this method to send token to your app server. Log.d(TAG, "sendRegistrationTokenToServer($token)") } private fun sendNotification(messageBody: String) { val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) val requestCode = 0 val pendingIntent = PendingIntent.getActivity( this, requestCode, intent, PendingIntent.FLAG_IMMUTABLE, ) val channelId = getString(R.string.default_notification_channel_id) val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) val notificationBuilder = NotificationCompat.Builder(this, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(getString(R.string.app_name)) .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_DEFAULT) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // Since android Oreo notification channel is needed. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( channelId, getString(R.string.default_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT, ) notificationManager.createNotificationChannel(channel) } val notificationId = 0 notificationManager.notify(notificationId, notificationBuilder.build()) } companion object { private const val TAG = "FCMService" } internal class MyWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { override fun doWork(): Result { // TODO(developer): add long running task here. return Result.success() } } }