android - 如何通过改造将 FCM 通知与博主一起使用

标签 android json firebase firebase-cloud-messaging retrofit

如果没有托管/自己的服务器
Managed by google 像 blogger 模板一样,我正在寻找设置 FCM 服务器协议(protocol)的方法,并设置项目的依赖项。
我在这个 question 中看到.一些答案包含类似于使用“okhttp3”的改造代码的代码

String SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
String FCM_ENDPOINT
     = "https://fcm.googleapis.com/v1/projects/zoftino-stores/messages:send";

GoogleCredential googleCredential = GoogleCredential
    .fromStream(new FileInputStream("firebase-private-key.json"))
    .createScoped(Arrays.asList(SCOPE));
googleCredential.refreshToken();
String token = googleCredential.getAccessToken();



final MediaType mediaType = MediaType.parse("application/json");

OkHttpClient httpClient = new OkHttpClient();

Request request = new Request.Builder()
    .url(FCM_ENDPOINT)
    .addHeader("Content-Type", "application/json; UTF-8")
    .addHeader("Authorization", "Bearer " + token)
    .post(RequestBody.create(mediaType, jsonMessage))
    .build();


Response response = httpClient.newCall(request).execute();
if (response.isSuccessful()) {
    log.info("Message sent to FCM server");
}

目前我在我的 android 应用程序中使用 blogger api 通过使用改造和 REST APIs 将 blogger 内容与它集成,作为 json 对象。 这是我用来检索博客的 BloggerAPI 类

public class BloggerAPI {

    public static final String BASE_URL =
            "https://www.googleapis.com/blogger/v3/blogs/2399953/posts/";
    public static final String KEY = "THE-KEY";

    public static PostService postService = null;

    public static PostService getService() {

        if (postService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            postService = retrofit.create(PostService.class);
        }

        return postService;
    }

    public interface PostService {
        @GET
        Call<PostList> getPostList(@Url String url);
    }
}

这样使用

private void getData(){

    String url = BloggerAPI.BASE_URL + "?key=" + BloggerAPI.KEY;

    if(token != ""){
        url = url+ "&pageToken="+token;
    }
    if(token == null){
        return;
    }

   final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(Call<PostList> call, Response<PostList> response) {
            PostList list = response.body();
            token = list.getNextPageToken();
            items.addAll(list.getItems());
            adapter.notifyDataSetChanged();
            Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<PostList> call, Throwable t) {
            Toast.makeText(MainActivity.this,"Error occured",Toast.LENGTH_LONG).show();
            Log.i(TAG, "onFailure: "+t.toString());
        }
    });

}

到目前为止,我已经成功设置了 firebase,它依赖于项目才能通过 firebase 控制台手动发送通知,我想做的是在我向博客发布新帖子时尝试自动发送通知

最佳答案

您需要使用 https://fcm.googleapis.com/fcm/send发送通知。这是 official documentation .

这是要导入和测试的 postman 集合 https://www.getpostman.com/collections/1d2e1f755d25f361c52f

关于android - 如何通过改造将 FCM 通知与博主一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550199/

相关文章:

mysql - 有没有办法将 MySQL 的 JSON_EXTRACT 与 JSON 字符串中的整数键一起使用?

json - JSONPath 中的 OR 运算符?

javascript - 根据 json 响应显示表格

android - React Native firebase 通知抬头未在某些设备中显示

android - 找不到字段的 setter - 将 Kotlin 与 Room 数据库结合使用

java - RecyclerView 上的 Android throw Action

android - Android xml 底部空间过大

android - 特殊字符Excel导入

使用 orderByChild 和 equalTo 来冰壶 Firebase?

ios - "An error occurred when accessing the keychain"