我试图将我的 cURL 用于 GCM 通知推送转换为改造功能,但我不知道具体如何。我的 cURL 如下所示:
curl --header "Authorization: key=KKK" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{'"registration_ids'":['"XXX'"]}"
其中 KKK = key,XXX = userRegistrationToken。
我在那里缺少消息,因为我没有找到如何做到这一点,但总的来说,这个 cURL 有效。我想做完全相同的事情,但有(通知)消息。
我尝试并得到了这样的东西:
//GCM_ENDPOINT = "https://android.googleapis.com";
@FormUrlEncoded
@POST("/gcm/send")
void sendNotification(
@Header("Authorization") String key,
@Header("Content-Type") String contentType,
@Field("message") String message,
Callback<Object> response);
到目前为止,我不知道这是否正确,但是它缺少我要发送的通知消息,我什至不知道如何处理这个-d registration_ids尤其是当我想在那里放几个id时。提前谢谢。
好的,我设法找到了一个解决方案,即使它不是通过直接转换该 cURL,它确实可以完美运行:
@POST("/gcm/send")
@Headers({"Content-Type: application/json"})
void sendNotification(
@Header("Authorization") String apiKey,
@Body TypedInput jsonBodyInput,
Callback<NotificationResult> response);
其中 NotificationResult 是我的自定义 gson 类,apiKey 必须带有"key="前缀(例如:字符串 apiKey = "key=" + "XXX";)。另外,重要的是将 jsonBodyInput 设置为像这样的类型输入:
TypedInput requestBody = new TypedByteArray("application/json", jsonString.getBytes(Charset.forName("UTF-8")));
jsonString 可能是什么样子的,你会在这里找到