I looking for the way to setup FCM Server Protocol if there's no hosting/own server Managed by google
like blogger template's, and setup the dependencies on project.
I see in this question. some answers contains code similar to retrofit codes using "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");
}
currently I using blogger api in my android app to integrate blogger content with it by using the retrofit and REST APIs, as a json objects. This a BloggerAPI class I used to retrieve blogs
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);
}
}
It is used thus
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());
}
});
}
Till now I succeeded on setup firebase and it's dependencies on the project to be able to send notification manually via firebase console, What I'm trying to do is try to automatically send notifications whenever I post a new post to the blog