-2

I am working on an android app in which client request for a job. When his job is completed I want to send a notification to submit feedback on Android App. Server Api needs to build in laravel and notifications will be send through Firebase. If you have any idea of helping material please share.

I have tried many youtube tutorials but no success. Most of them use custom php apis but i need in laravel and send notification to a specific user.

Thanks!

Abdullah Javed
  • 459
  • 3
  • 17
  • i think this doesn't depend on langauge specific like core php. it's just a post request(to firebase url, with server key, with users push tokens array, and message). – SRB Bans May 13 '19 at 09:50

2 Answers2

0

first of all you get devicetoken from firebase and then send notification from application using this below code.

public class UtilsFcm {


static OkHttpClient mClient;
static Context context;
static JSONArray jsonArray;


public static void sendNotification(final Context context1, final JSONArray jsonArray1) {

    mClient = new OkHttpClient();
    context = context1;  
    jsonArray = jsonArray1;

    new MyTask().execute();

}


static class MyTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {


        try {

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject root = new JSONObject();
                JSONObject notification = new JSONObject();
                notification.put("text", "Your notification message");                                     
                notification.put("title", "App Title");
                notification.put("line1", R.mipmap.ic_launcher);
                notification.put("line2", "high");

                root.put("to", jsonArray.get(i));
                root.put("data", notification);


                String result = postToFCM(root.toString());
                Log.d("Main Activity", "Result: " + result);
                return result;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(String result) {

        if (result != null) {
            try {
                JSONObject resultJson = new JSONObject(result);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context, "" + e.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}


static String postToFCM(String bodyString) throws IOException {

    final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";

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

    RequestBody body = RequestBody.create(JSON, bodyString);
    Request request = new Request.Builder()
            .url(FCM_MESSAGE_URL)
            .post(body)
            .addHeader("Authorization", "key=" + "firebase_web_api_key")
            .build();
    Response response = mClient.newCall(request).execute();
    return response.body().string();
}

}

and then you call this method like this.
UtilsFcm.sendNotification(this,jsonArray);
here jsonarray have all device token.
hope this will help you.
Vishnu Saini
  • 129
  • 5
0

First of all you need to grab the InstanceToken from the app in the frontend and submit it to your backend somehow.

After you have that you can send notifications from the backend using Firebase. Have a look at this great package for some guidance on how that can be done: https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html

user1415066
  • 855
  • 7
  • 11