Skip to content

Instantly share code, notes, and snippets.

@ssniks
Created May 6, 2022 14:35
Show Gist options
  • Save ssniks/5ac6ce87086b9c38f3296f0e135452f6 to your computer and use it in GitHub Desktop.
Save ssniks/5ac6ce87086b9c38f3296f0e135452f6 to your computer and use it in GitHub Desktop.
Firebase Service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
try {
val channelName = "General"
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(NotificationChannel(channelName,channelName,NotificationManager.IMPORTANCE_DEFAULT))
}
notificationManager.notify(
1,
NotificationCompat.Builder(this,"")
.setContentTitle(remoteMessage.notification?.title?:"Title")
.setContentText(remoteMessage.notification?.body?:"Body")
.build()
)
} catch (e: Exception) {
Log.e(TAG, "onMessageReceived", e)
}
}
override fun onNewToken(token: String) {
try {
Log.i(TAG, "FCM Token : $token")
} catch (e: Exception) {
Log.e(TAG, "onNewToken", e)
}
}
companion object {
const val TAG = "fcm"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment