通过改造android(kotlin)向特定主题发送fcm消息
我试图通过改装发送fcm中特定主题的消息。我遵循fcm中的说明,但我不断收到http请求的404代码。这是我的代码: 常量文件:通过改造android(kotlin)向特定主题发送fcm消息,android,firebase,kotlin,firebase-cloud-messaging,retrofit,Android,Firebase,Kotlin,Firebase Cloud Messaging,Retrofit,我试图通过改装发送fcm中特定主题的消息。我遵循fcm中的说明,但我不断收到http请求的404代码。这是我的代码: 常量文件: class Constans { companion object{ const val BASE_URL = "https://fcm.googleapis.com" const val CONTENT_TYPE ="application/json" const val SERVER_KEY =&
class Constans {
companion object{
const val BASE_URL = "https://fcm.googleapis.com"
const val CONTENT_TYPE ="application/json"
const val SERVER_KEY ="myKey"
}
}
通知API:
interface NotificationApi {
@Headers("Authorization: key=$SERVER_KEY", "Content-Type:${CONTENT_TYPE}")
@POST("fcm/send")
suspend fun postNotification(
@Body message: Message
): Response<ResponseBody>
}
消息类:
data class Message(
var to:String,
var data:Notification,
)
通知类别:
data class Notification (
val body:String="",
val title:String=""
)
发送通知功能:
private fun sendNotification(body:String,title:String)= CoroutineScope(Dispatchers.IO).launch {
try {
//orders is the topic name
val message = Message("orders",Notification(body,title))
val response = RetrofitInstance.api.postNotification(message)
if (response.isSuccessful) {
Log.e("msg1", response.message())
} else {
//here i get 404
Log.e("msg", response.code().toString())
}
} catch (e: Exception) {
Log.e("Error", e.message.toString())
}
}
我不确定这一点,但是如果将
通知Api-@Headers
更改为
interface NotificationApi {
@Headers("Authorization: Bearer $SERVER_KEY", "Content-Type:${CONTENT_TYPE}")
@POST("fcm/send")
suspend fun postNotification(
@Body message: Message
): Response<ResponseBody>
接口通知API{
@标题(“授权:承载$SERVER\u KEY”,“内容类型:${Content\u Type}”)
@邮政(“fcm/发送”)
暂停娱乐后通知(
@正文消息:消息
):回应
您是否尝试这样修改代码
interface NotificationApi {
@Headers("Authorization: key=$SERVER_KEY", "Content-Type:$CONTENT_TYPE")
@POST("fcm/send")
suspend fun postNotification(
@Body message: Message
): Response<ResponseBody>
接口通知API{
@标题(“授权:密钥=$SERVER\u密钥”,“内容类型:$Content\u类型”)
@邮政(“fcm/发送”)
暂停娱乐后通知(
@正文消息:消息
):回应
没有任何事情发生相同的错误,我尝试对另一个应用程序执行相同的步骤,并将其链接到同一个firebase项目,它工作得非常完美,这是我一生中遇到的最奇怪的错误。
interface NotificationApi {
@Headers("Authorization: key=$SERVER_KEY", "Content-Type:$CONTENT_TYPE")
@POST("fcm/send")
suspend fun postNotification(
@Body message: Message
): Response<ResponseBody>