Created
March 30, 2021 16:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MessagingServices: DaggerFirebaseService() { | |
override fun onNewToken(token: String) { | |
// whatever you want | |
} | |
override fun onMessageReceived(remoteMessage: RemoteMessage) { | |
super.onMessageReceived(remoteMessage) | |
// whatever you want | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Click here for the Java (without dagger) version