从 Android 设备通过 Firebase 发送推送通知

问题描述 投票:0回答:2

我有兴趣通过 Firebase 从 Android 应用程序发送推送通知。 Firebase 文档指出必须将有效负载 json 发送到 https://fcm.googleapis.com/fcm/send

因此,我使用 Retrofit 发送 json 但请求失败,如果我在不使用 Retrofit 的情况下发送请求,则效果完美。

这两种实现之间有什么区别以及为什么它不能与 Retrofit 一起使用。

以下是两种实现以及使用Retrofit时的错误。

val notificationBody = NotificationBody(notificationData, "/topics/all")
    val rb = Gson().toJson(notificationBody).toRequestBody("application/json".toMediaTypeOrNull())
    val responeNoti = RetrofitManager.notificationServiceApi.postNotification(rb)
    responeNoti.enqueue(object : Callback<ResponseBody>{
        override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
            Log.d(TAG, t.message)
        }

        override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
            Log.d(TAG, response.toString())
        }

    })

通知服务API

 private const val FCM_URL = "https://fcm.googleapis.com/fcm/"
val notificationServiceApi: ServiceApi by lazy{
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val okHttpClientBuilder = OkHttpClient.Builder()
        okHttpClientBuilder.addInterceptor(object : Interceptor{
            override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
                val request: Request = chain.request().newBuilder().
                addHeader("Content-Type", "application/json").
                addHeader("Authorization","key=*my firebase key*").build()

                return chain.proceed(request)
            }
        }).addInterceptor(interceptor)

        val notificationRetrofit = Retrofit.Builder()
            .client(okHttpClientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(FCM_URL)
            .build()
        notificationRetrofit.create(ServiceApi::class.java)
    }

服务API

@POST("send/")
fun postNotification(@Body notificationBody: RequestBody) : Call<ResponseBody>

日志Http

2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: --> POST 
        https://fcm.googleapis.com/fcm/send/
2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: Content- 
Length: 117
    2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: Content-Type: application/json
    2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: Authorization: key=my firebase key
    2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: {"data":{"imageUrl":"https://i.imgur.com/KA1jrU7.jpg","textBody":"my content","title":"my title"},"to":"/topics/all"}
    2020-09-06 00:12:27.991 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: --> END POST (117-byte body)
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: <-- 404 https://fcm.googleapis.com/fcm/send/ (1206ms)
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: content-type: text/plain; charset=utf-8
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: x-content-type-options: nosniff
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: x-frame-options: SAMEORIGIN
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: x-xss-protection: 0
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: date: Sat, 05 Sep 2020 21:12:29 GMT
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: content-length: 135
    2020-09-06 00:12:29.198 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: alt-svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
    2020-09-06 00:12:29.199 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: A valid push subscription endpoint should be specified in the URL as such: https://fcm.googleapis.com/wp/dHIoDxE7Hdg:APA91bH1Zj0kNa...
    2020-09-06 00:12:29.199 32059-32244/com.bagi.soreknetmanager I/okhttp.OkHttpClient: <-- END HTTP (135-byte body)
    2020-09-06 00:12:29.201 32059-32059/com.bagi.soreknetmanager D/MainActivity: Response{protocol=h2, code=404, message=, url=https://fcm.googleapis.com/fcm/send/}

无需使用 Retrofit 即可实现出色的实现

 val notificationBody = NotificationBody(notificationData, "/topics/all")
    val jsonPayload = createJsonFromObject(notificationBody)
     val thread = Thread(Runnable {
    try {
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        var client = OkHttpClient().newBuilder().addInterceptor(interceptor)
            .build()
        var mediaType = "application/json".toMediaTypeOrNull()
        var body: RequestBody = RequestBody.create(
            mediaType,
            jsonPayload
        )
        var request: Request = Request.Builder()
            .url("https://fcm.googleapis.com/fcm/send")
            .method("POST", body)
            .addHeader("Content-Type", "application/json")
            .addHeader(
                "Authorization",
                "key=my firebase key"
            )
            .build()
        var response: okhttp3.Response = client.newCall(request).execute()
    } catch (e: Exception) {
        e.printStackTrace()
    }
})

thread.start()
android firebase push-notification retrofit okhttp
2个回答
0
投票

您的网址有错误.. 检查您的 FCM_URL,它不完整,您忘记附加“发送”一词

FCM_URL = "https://fcm.googleapis.com/fcm/"

正确的网址-

FCM_URL ="https://fcm.googleapis.com/fcm/send"

0
投票

观看此 HTTP v1 视频 升级您的推送通知:从旧版 FCM 迁移到 HTTP v1

© www.soinside.com 2019 - 2024. All rights reserved.