i'm trying to send notification from android using retrofit and i followed the offical documentation from firebase. i setup retrofit with https://fcm.googleapis.com/fcm/send and when i post notifications retrofit returns response but the message dosen't appear in my logs. i think my service is not working after the message was posted.
MainActivity
const val TOPIC = "MyTopic"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseMessaging.getInstance().subscribeToTopic(TOPIC)
val btnSend = findViewById<Button>(R.id.btnSend)
btnSend.setOnClickListener {
FirebaseNotificationData(
NotificationData("Notification title", "Notification message"),
TOPIC
).also {
postNotification(it)
}
}
}
private fun postNotification(notification: FirebaseNotificationData) =
CoroutineScope(Dispatchers.IO).launch {
val response = RetrofitInstance.api.postNotification(notification)
if (response.isSuccessful)
Log.d("Retrofit", "Message was posted")
else
Log.e("Retrofit", response.errorBody().toString())
}
}
Service
class FirebaseNotificationService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Log.d("onMessageReceived","message received")
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firebasenotification">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FirebaseNotification">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebaseNotificationService"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
</application>
</manifest>