Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FCM notification with blogger using retrofit

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

like image 779
Dr Mido Avatar asked Feb 06 '19 09:02

Dr Mido


People also ask

How do I use FCM for push notifications?

For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.

Is FCM and GCM the same?

Firebase Cloud Messaging (FCM), formerly known as Google Cloud Messaging (GCM), is a cross-platform cloud solution for messages and notifications for Android, iOS, and web applications, which as of June 2022 can be used at no cost.


1 Answers

You need to use https://fcm.googleapis.com/fcm/send to send notification. Here is the official documentation.

Here is the postman collection to import and test https://www.getpostman.com/collections/1d2e1f755d25f361c52f

like image 108
Hammad Akram Avatar answered Oct 19 '22 12:10

Hammad Akram