Hello guys! I am trying to implement firebase push...
# android
j
Hello guys! I am trying to implement firebase push notifications but when the app is in open, the notification does not come in. This is my FirebaseService class
class FirebaseService : FirebaseMessagingService() {
init {
Log.d("TAG", "onMessageReceived: ")
}
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d("TAG", "onNewToken: $token")
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Log.d("TAG", "onMessageReceived: ${message._notification_?._title_}")
}
}
I have also added this class to my manifest as shown below
<service
android:name=".FirebaseService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGIN_EVENT"/>
</intent-filter>
</service>
However, when the app is in the background, I receive notification on the notification tray but when the app is open, I expect to see the log message in the onMessageReceived method which never triggers. What am i missing?
not kotlin but kotlin colored 4
j
Check that you have runtime permissions POST_NOTIFICATION enabled for the app. Recommend using it to check if its not enabled when want to show notification, trigger user to allow it again 🙂
j
Yes, I do
j
Then not sure, a lot of issues can occur in this topic, which is way out of topic from this Slack 😛 Check all variants here https://firebase.google.com/docs/cloud-messaging/android/client and double check not missing anything.
f
If I remember correctly; The onMessageReceived, is for ‘data messages’ and not ‘notification messages’. There seems to be a difference in Firebase. You can however combine, to have a ‘notification message with data’. But please consult the docs, as it’s a long time ago when I last looked at Firebase Messaging.
j
@Frank Bouwens They are received in same method, and check by using remoteMessage.data vs remoteMessage.notification. See https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived Pain thing is to handle cold boot vs not, multiple states how to handle what to happen in the app for each. And then gets more fun if want to do Kotlin multiplatform 😄
f
Aha! I almost remembered correctly 😅 It can get more complex fast; Good luck! Especially on multitplatform 😉
j
Yeah getting nightmares each time I want notifications in a new app and try to explain to backend developers how to distinguish data vs not and different notification channels in iOS vs Android 😄