如何使用Retrofit从Android发送FCM通知?

5

我希望能够使用retrofit从安卓设备向另一个设备发送fcm通知

我尝试了以下方法,但是:

public interface ApiInterface {
    @Headers("Authorization : key=AAAA4Ubio1Q:APA91bGWkw84b1XX2nnnOKn8MO25U2giLRXXXTUkXidojFluZk_qKXXXlS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEEX26VFDDbXN8")
    @POST("fcm/send")
    Call<ResponseBody> sendChatNotification(@Field("to") String token,@Body RequestNotificaton requestNotificaton);

}

我遇到了这个错误。
java.lang.IllegalArgumentException: Unexpected char 0x20 at 13 in header name: Authorization 
        at okhttp3.Headers$Builder.checkNameAndValue(Headers.java:330)
        at okhttp3.Headers$Builder.add(Headers.java:288)
        at retrofit2.ServiceMethod$Builder.parseHeaders(ServiceMethod.java:329)
        at retrofit2.ServiceMethod$Builder.parseMethodAnnotation(ServiceMethod.java:270)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:175)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:913)
        at $Proxy0.sendChatNotification(Unknown Source)
        at com.rudruam.v2chat.Activity.ChatActivity.sendNotificationToPatner(ChatActivity.java:288)
        at com.rudruam.v2chat.Activity.ChatActivity.access$400(ChatActivity.java:94)
        at com.rudruam.v2chat.Activity.ChatActivity$4.onClick(ChatActivity.java:227)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6499)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:442)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我认为在Header中有一个错误

注意:我看到了https://github.com/square/retrofit/issues/1153,但对我没有用。

1个回答

12

问题终于解决了!

这是在header中犯的错误,实际上应该像这样:

@Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1XX2nnnOKn8MO25U2giLRXXXTUkXidojFluZk_qKXXXlS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEEX26VFDDbXN8",
            "Content-Type:application/json"})

以下是如何使用retrofit从Android发送fcm通知的完整代码:

public interface ApiInterface {
    @Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1Pw2nnnOKn8MO25U2giLRtv5TUkXidojFluZk_qKOGllS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEED26VFDDbXN8",
            "Content-Type:application/json"})
    @POST("fcm/send")
    Call<ResponseBody> sendChatNotification(@Body RequestNotificaton requestNotificaton);
}

ApiClient类:

public static final String BASE_URL = "https://fcm.googleapis.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

模型类:RequestNotification.class

public class RequestNotificaton {

    @SerializedName("token") //  "to" changed to token
    private String token;

    @SerializedName("notification")
    private SendNotificationModel sendNotificationModel;

    public SendNotificationModel getSendNotificationModel() {
        return sendNotificationModel;
    }

    public void setSendNotificationModel(SendNotificationModel sendNotificationModel) {
        this.sendNotificationModel = sendNotificationModel;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

模型类:SendNotificationModel.class

(该类与发送通知相关)
public class SendNotificationModel {
    private String body,title;

    public SendNotificationModel(String body, String title) {
        this.body = body;
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

主要代码:

private void sendNotificationToPatner() {

        SendNotificationModel sendNotificationModel = new SendNotificationModel("check", "i miss you");
        RequestNotificaton requestNotificaton = new RequestNotificaton();
        requestNotificaton.setSendNotificationModel(sendNotificationModel);
        //token is id , whom you want to send notification ,
        requestNotificaton.setToken(token);

        apiService =  ApiClient.getClient().create(ApiInterface.class);
        retrofit2.Call<ResponseBody> responseBodyCall = apiService.sendChatNotification(requestNotificaton);

        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                Log.d("kkkk","done");
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

            }
        });
    }

我希望能够帮到您! 注意 依赖关系为:
// retrofit, gson
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

我已添加了上述代码,但无法收到通知,我应该怎么办? - Mr. Roshan
你得到了什么?尝试使用调试! - iamkdblue
实际上,我在日志中没有得到任何信息,通知也没有发送?还有一件事,我在代码中做了一个更改,即删除了requestNotificaton.setToken(token);这行代码,因为我想向所有应用程序实例发送通知。 - Mr. Roshan
我们需要获取安装了应用程序的不同设备的所有令牌。 - iamkdblue
你能分享你的Whatsapp号码吗? - Mr. Roshan
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接