如何向 Android 应用程序的不同用户发送通知

How to send a notification to a distinct user of Android App

我正在开发一个 android 应用程序,客户在其中请求工作。当他的工作完成后,我想发送通知以提交对 Android 应用程序的反馈。服务器 Api 需要在 laravel 中构建,通知将通过 Firebase 发送。如果您有任何帮助 material 的想法,请分享。

我尝试了很多 youtube 教程但没有成功。他们中的大多数使用自定义 php api,但我需要 laravel 并向特定用户发送通知。

谢谢!

首先,您从 firebase 获取 devicetoken,然后使用以下代码从应用程序发送通知。

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.

首先,您需要从前端的应用程序中获取 InstanceToken,然后以某种方式将其提交到您的后端。

完成后,您可以使用 Firebase 从后端发送通知。看看这个很棒的软件包,以获得有关如何完成的一些指导: https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html