9

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

Pratik Butani
  • 51,868
  • 51
  • 228
  • 375
Dr Mido
  • 2,017
  • 2
  • 15
  • 41

3 Answers3

1

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

Hammad Akram
  • 513
  • 3
  • 9
0

I haven't used blogger API before but as you said that you are creating post via Blogger API's, I can give you a clue to solve your problem.

For example, you have created one API in PHP to send notification (all users, at a time 1000 that you have to manage), You can test using this link

I saw how Adding Post is working. It is giving you success response when you add new post.

You can call PHP API of Notification when you get success response after adding post.

Hope you will convert my words to code. It will help you forsure.

Do let me know, If you have any question.

Pratik Butani
  • 51,868
  • 51
  • 228
  • 375
  • I think there's a misunderstanding. I'm using the blogger API to fetch list of posts I've posted on it and not to post new posts, here's the tutorial that I followed "Android Complete App Development Hindi Tutorials - हिंदी में." https://www.youtube.com/playlist?list=PLRKyZvuMYSIOFU_38SBsu3JVnjjvgx-67 in the video X he explain how to set up FCM notifier to send notifications from the console only, https://firebase.google.com/docs/admin/setup in this link guide to "Add the Firebase Admin SDK" to the Server, they said : – Dr Mido Feb 15 '19 at 11:49
  • The Admin SDK lets you interact with Firebase from privileged environments to perform actions like: "Programmatically send Firebase Cloud Messaging messages using a simple, alternative approach to the FCM server protocols." this is what I needed, but as you know the blogger platform is managed by google as many users of it there's no need to buy host service, all UI and codes located inside blogger template html, So I'm looking for a way to install the Firebase Admin SDK tools on it and try sending automatic notification via firebase to users, I apologize to the prolongation – Dr Mido Feb 15 '19 at 11:59
  • 1
    You want to send notification when you create new post, right? and That post you are creating in Blogger of Google. So you can do one thing, create trigger for database and call it when you add new post. – Pratik Butani Feb 15 '19 at 12:48
  • Yes exactly this is, I'm already using an ormlite database in the app, I do not know did you mean firebase realtime database? – Dr Mido Feb 15 '19 at 14:05
-2

You can use send FCM notification to the subscribers of blog . reference:- FCM topic messaging

rupali mittal
  • 156
  • 1
  • 8