setup FCM in Android

## Add Firebase to an existing android project
Click <b>Tools</b> -> <b>Firebase</b>
<img width="1345" alt="Screen Shot 2019-06-11 at 18.29.36.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211892/ca77cfc7-f826-c97d-43c4-3eed4b983d4e.png">
A new firebase sidebar will appear. From that click <b>Set up Firebase Cloud Messaging</b>
<img width="804" alt="Screen Shot 2019-06-11 at 18.31.40.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211892/bea84c23-90ea-2d21-d323-b7d22e37cdd5.png">
Next click on <b>Connect to Firebase</b>. It will ask to login to firebase console with gmail account. Then follow the instructions.
After that click on <b>Add FCM to your app</b> then follow the instructions.

## Receiving message from FCM
Create a new kotlin class called `MyFirebaseMessagingService.kt` and add this following code:

```kotlin
class MyFirebaseMessagingService: FirebaseMessagingService() {
    val TAG = "FirebaseMessagingService"

    override fun onNewToken(token: String?) {
        super.onNewToken(token)
        Log.d("TOKEN", token)
    }

    @SuppressLint("LongLogTag")
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        Log.d(TAG, remoteMessage.from)

        if (remoteMessage.notification != null) {
            showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
        }
    }

    // show notification
    private fun showNotification(title: String?, body: String?) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(0, notificationBuilder.build())
    }
}
```

Then from `AndroidManifest.xml` add this service:

```xml
<service
   android:name=".MyFirebaseMessagingService">
   <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT"/>
   </intent-filter>
</service>
```

## Push notification from the console dashboard
In the first page, scroll down a little bit and choose <b>Cloud Messaging</b>
![1*2D-nqPhKEixysXp1k-3xog.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211892/f302e9b8-4363-4824-2461-066602e062c2.png)
Inside the new page, click <b>Send your first message</b>
![1*wFx-H0_60Rt4S5V548SJYA.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211892/d3c05630-656c-d691-a865-ad9adda52114.png)
Inside <b>Message Text</b>, write the information you want the user to know (the long message). In <b>Target</b> choose <b>User segment</b> and in <b>Select app</b> choose the application package name. Next (optional).
After finish click <b>Review</b> then <b>Publish</b>

## Trigger push notification
So by default, we will set FirebaseMessaging to automatically subscribe to topic `GENERAL`

```kotlin
FirebaseMessaging.getInstance().subscribeToTopic("GENERAL")
```
Then when we want to turn off the notification, we just need to unsubscribe from this topic

```kotlin
FirebaseMessaging.getInstance().unsubscribeFromTopic("GENERAL")
```

Please refer to this <a href="https://github.com/phanithken/AndroidTogglePermission.git">Github link</a>
Thank you!