Skip to content

Instantly share code, notes, and snippets.

@thenishchalraj
Created March 30, 2021 16:38
Show Gist options
  • Save thenishchalraj/dbfc5b391d4d98680672053ac190d51b to your computer and use it in GitHub Desktop.
Save thenishchalraj/dbfc5b391d4d98680672053ac190d51b to your computer and use it in GitHub Desktop.
[Kotlin](used dagger) MessagingServices extends FirebaseMessageService to get the FCM Device Token when the app is installed for the first time.
class BaseApplication: DaggerApplication() {
@Inject
lateinit var mSharedPrefs: SharedPreferences
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.factory().create(this)
}
override fun onCreate() {
super.onCreate()
// important
FirebaseApp.initializeApp(this)
FirebaseInstallations.getInstance().id.addOnCompleteListener { task: Task<String?> ->
if (task.isSuccessful) {
val token = task.result
Log.e("token ---->>", token!!)
val mEditor = mSharedPrefs.edit()
mEditor.putString(AppConstants.FCM_TOKEN, token)
mEditor.apply()
}
}
}
}
// if you don't want to use dagger just extend the MessagingServices directly with FirebaseMessagingService()
abstract class DaggerFirebaseService : FirebaseMessagingService() {
override fun onCreate() {
AndroidInjection.inject(this)
super.onCreate()
}
}
class MessagingServices: DaggerFirebaseService() {
override fun onNewToken(token: String) {
// whatever you want
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
// whatever you want
}
}
@thenishchalraj
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment