如果没有像博客模板那样的托管/自己的服务器Managed by google
,我正在寻找设置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 API作为json对象,将Blogger内容与其集成在一起。
这是我用来检索博客的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