title: Firebase Cloud Messaging Android using Kotlin
body: <p>I am trying to send cloud messages (push notifications) from the firebase console to my app. I have seen similar questions on here but nothing worked for me.
  I followed the documentation, watched the tutorial, searched online, and StackOverflow and nothing seems to work for me. MainActivity, I am getting the token and trying to use it to send a test message.</p>
  <pre><code>FirebaseInstanceId.getInstance().instanceId
              .addOnCompleteListener(OnCompleteListener { task -&gt;
                  if (!task.isSuccessful) {
                      Log.w(TAG, &quot;getInstanceId failed&quot;, task.exception)
                      return@OnCompleteListener
                  }
  
                  // Get new Instance ID token
                  val token = task.result?.token
  
                  // Log and toast
                  val msg = getString(R.string.msg_token_fmt, token)
                  Log.d(TAG, msg)
              })
  </code></pre>
  <p>MyFirebaseMessagingService similar to the code from the documenttaion</p>
  <pre><code>class MyFirebaseMessagingService : FirebaseMessagingService() {
  
      override fun onMessageReceived(remoteMessage: RemoteMessage) {
          // [START_EXCLUDE]
          // There are two types of messages data messages and notification messages. Data messages are handled
          // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
          // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
          // is in the foreground. When the app is in the background an automatically generated notification is displayed.
          // When the user taps on the notification they are returned to the app. Messages containing both notification
          // and data payloads are treated as notification messages. The Firebase console always sends notification
          // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
          // [END_EXCLUDE]
  
          Log.e(TAG, &quot;From: ${remoteMessage.from}&quot;)
  
          // Check if message contains a data payload.
          if (remoteMessage.data.isNotEmpty()) {
              Log.d(TAG, &quot;Message data payload: ${remoteMessage.data}&quot;)
  
              if (/* Check if data needs to be processed by long running job */ true) {
                  // 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, &quot;Message Notification Body: ${it.body}&quot;)
  
              sendNotification(it.body.toString())
          }
  
          // 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]
  
      // [START on_new_token]
      /**
       * Called if InstanceID token is updated. This may occur if the security of
       * the previous token had been compromised. Note that this is called when the InstanceID token
       * is initially generated so this is where you would retrieve the token.
       */
      override fun onNewToken(token: String) {
          Log.d(TAG, &quot;Refreshed token: $token&quot;)
  
          // If you want to send messages to this application instance or
          // manage this apps subscriptions on the server side, send the
          // Instance ID token to your app server.
          sendRegistrationToServer(token)
      }
      // [END on_new_token]
  
      /**
       * Schedule async work using WorkManager.
       */
      private fun scheduleJob() {
          // [START dispatch_job]
          val work = OneTimeWorkRequest.Builder(MyWorkerActivity::class.java).build()
          WorkManager.getInstance().beginWith(work).enqueue()
          // [END dispatch_job]
      }
  
      /**
       * Handle time allotted to BroadcastReceivers.
       */
      private fun handleNow() {
          Log.d(TAG, &quot;Short lived task is done.&quot;)
      }
  
      /**
       * Persist token to third-party servers.
       *
       * Modify this method to associate the user's FCM InstanceID token with any server-side account
       * maintained by your application.
       *
       * @param token The new token.
       */
      private fun sendRegistrationToServer(token: String?) {
          // TODO: Implement this method to send token to your app server.
          Log.d(TAG, &quot;sendRegistrationTokenToServer($token)&quot;)
      }
  
      /**
       * Create and show a simple notification containing the received FCM message.
       *
       * @param messageBody FCM message body received.
       */
      private fun sendNotification(messageBody: String) {
          val intent = Intent(this, MainActivity::class.java)
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
          val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
              PendingIntent.FLAG_ONE_SHOT)
  
          val channelId = getString(R.string.default_notification_channel_id)
          val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
          val notificationBuilder = NotificationCompat.Builder(this, channelId)
              .setSmallIcon(R.drawable.ic_stat_sample_notification)
              .setContentTitle(getString(R.string.fcm_message))
              .setContentText(messageBody)
              .setAutoCancel(true)
              .setSound(defaultSoundUri)
              .setContentIntent(pendingIntent)
  
          val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  
          // Since android Oreo notification channel is needed.
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              val channel = NotificationChannel(channelId,
                  &quot;Channel human readable title&quot;,
                  NotificationManager.IMPORTANCE_DEFAULT)
              notificationManager.createNotificationChannel(channel)
          }
  
          notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
      }
  
      companion object {
  
          private const val TAG = &quot;MyFirebaseMsgService&quot;
      }
  }
  </code></pre>
  <p>And inside my AndroidManifest file I have this</p>
  <pre><code>&lt;!-- [START firebase_service] --&gt;
          &lt;service
              android:name=&quot;.ui.services.MyFirebaseMessagingService&quot;
              android:exported=&quot;false&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;!-- [END firebase_service] --&gt;
  </code></pre>
  <p>I saw a similar question that had an answer to downgrading the firebase messaging dependency, I also tried that with no luck. I will post more If I have to. Any help will be much appreciated, thanks.</p>