如何使用 Firebase 中的用户令牌向特定用户发送通知?

How can I send notification to specific User using the user's token in Firebase?

我正在开发一个 android 应用程序 (java),我正在使用 Firebase,对于每个注册用户,我都有一个设备令牌,如何向特定用户发送通知使用他的令牌 ?

在这里使用这个 YouTube link EDMT Dev 在他的 Eat it 新系列中实现了以下内容。如果对您有帮助,请将此标记为正确答案。

添加下面的依赖

 `implementation 'io.reactivex.rxjava2:rxandroid:2.1.1`'

现在创建这些 classes :

令牌模型 该模型用于检索令牌数据(令牌,Phone)。我还有一个 phone 的变量,因为我根据我的数据结构制作了这个 class 。请根据需要修改代码

public class TokenModel {

private String phone,token;

public TokenModel() {
}

public TokenModel(String phone, String token) {
    this.phone = phone;
    this.token = token;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}}

FCM 发送数据模型

  public class FCMSendData {
private String to;
private Map<String,String> data;

public FCMSendData(String to, Map<String, String> data) {
    this.to = to;
    this.data = data;
}

public String getTo() {
    return to;
}

public void setTo(String to) {
    this.to = to;
}

public Map<String, String> getData() {
    return data;
}

public void setData(Map<String, String> data) {
    this.data = data;
}}

创建 FCM 结果模型

 class FCMResult {
    private String message_id;
    
    public FCMResult() {
    }
    
    public String getMessage_id() {
        return message_id;
    }
    
    public void setMessage_id(String message_id) {
        this.message_id = message_id;
    }}

创建 RetrofitFCMClient

public class RetrofitFCMClient {
private static Retrofit instance;
public static Retrofit getInstance()
{
    if(instance==null)
    {
        instance  = new Retrofit.Builder().baseUrl("https://fcm.googleapis.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return instance;
    }
    return instance;
}}

现在我们需要实现一个接口 //授权密钥是云消息的服务器密钥

public 接口 IFCMService {

@Headers({
        "Content-Type:application/json",

        "Authorization:key=**YOUR_AUTHORIZATION KEY HERE**"

})
@POST("fcm/send")
Observable<FCMResponse> sendNotification(@Body FCMSendData body);}

现在我们已经准备好使用 firebase 消息传递,只需要放置数据并使用我们的改造来推送它

TokenModel tokenModel = dataSnapshot.getValue(TokenModel.class);
                                        //("FCM",tokenModel.getToken());
                                        Map<String, String> notiData = new  
HashMap<>();
             notiData.put(Common.NOTI_TITLE, "YOUR NOTIFICATION TITLE");
             notiData.put(Common.NOTI_CONTENT,"YOUR_NOTFICATION CONTENT );

    FCMSendData sendData = new FCMSendData(tokenModel.getToken(), 
notiData);
                                        
  compositeDisposable
 .add(ifcmService.sendNotification(sendData)
  .subscribeOn(Schedulers.io()).
                                           


  observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Consumer<FCMResponse>() {
                                        @Override
                                        public void accept(FCMResponse 
fcmResponse)              

     throws Exception {
                                            if (fcmResponse.getSuccess()  
== 1) {

                                                 
Toast.makeText(getContext(),     

    "Success", Toast.LENGTH_LONG).show();
                                            } else {
                                                 
Toast.makeText(getContext(), "Failed  
       to Notify", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    }, new Consumer<Throwable>() {
                                        @Override
                                        public void accept(Throwable 
throwable) throws Exception {
                                            Toast.makeText(getContext(),   
throwable.getMessage(), Toast.LENGTH_LONG).show();

                                        }
                                    }));




    

在这里使用这个YouTube link EDMT Dev 在他的 Eat it 新系列中实现了以下内容

要向用户发送通知,唯一需要的就是该用户的令牌。您可以使用 FCM 发送通知。 在这里,我分享了我的 FCM class 可用于此目的。它使用 Okhttp3 请求,因此请确保添加它的依赖项。

    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'

添加这个依赖后,你所要做的就是使用这个FCM class。

FCMMessages.java

public class FCMMessages {

    private Context context;

    public void sendMessageSingle(Context context, final String recipient, final String title, final String body, final Map<String, String> dataMap)
    {
        this.context = context;

        Map<String, Object> notificationMap = new HashMap<>();
        notificationMap.put("body", body);
        notificationMap.put("title", title);

        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("notification", notificationMap);
        rootMap.put("to", recipient);
        if (dataMap != null)
            rootMap.put("data", dataMap);

        new SendFCM().setFcm(rootMap).execute();
    }


    public void sendMessageMulti(Context context, final JSONArray recipients, final String title, final String body, final Map<String, String> dataMap) {
        this.context = context;

        Map<String, Object> notificationMap = new HashMap<>();
        notificationMap.put("body", body);
        notificationMap.put("title", title);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("notification", notificationMap);
        rootMap.put("registration_ids", recipients);
        if (dataMap != null)
            rootMap.put("data", dataMap);

        new SendFCM().setFcm(rootMap).execute();
    }

    @SuppressLint("StaticFieldLeak")
    class SendFCM extends AsyncTask<String, String, String> {

        private String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
        private Map<String, Object> fcm;

        SendFCM setFcm(Map<String, Object> fcm) {
            this.fcm = fcm;
            return this;
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                RequestBody body = RequestBody.create(JSON, new JSONObject(fcm).toString());
                Request request = new Request.Builder()
                        .url(FCM_MESSAGE_URL)
                        .post(body)
                        .addHeader("Authorization","key=" + StaticConfig.myMessagingAuth)
                        .build();
                Response response = new OkHttpClient().newCall(request).execute();
                return response.body().string();

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            try {
                JSONObject resultJson = new JSONObject(result);
                int success, failure;
                success = resultJson.getInt("success");
                failure = resultJson.getInt("failure");
            //Toast.makeText(context, "Sent: " + success + "/" + (success + failure), Toast.LENGTH_LONG).show();
            } catch (JSONException e) {
                e.printStackTrace();
//                Toast.makeText(context, "Message Failed, Unknown error occurred.", Toast.LENGTH_LONG).show();
            }
        }
    }

}

确保从您的 firebase 项目设置中获取 messagingAuth。要获取 messagingAuth 令牌,请执行以下步骤:

Open Firebase Project > Project Settings > Cloud Messaging > Server key

复制 服务器密钥 的值并将其作为 messagingAuth 粘贴到您的 android 项目中。

要向单个用户令牌发送通知,请使用 sendMessageSingle 方法。就像

String user_token = "wiubd92uhe91dik-q";
String notification_title = "This is notification title";
String notification_des = "This is notification description";
new FCMMessages().sendMessageSingle(MainActivity.this, user_token, notification_title, notification_des, null);

要向多个用户令牌发送通知,请使用 sendMessageMulti 方法。就像

ArrayList<String> user_tokens = new ArrayList<>();
user_tokens.add(token_1);
user_tokens.add(token_2);
user_tokens.add(token_3);
String notification_title = "This is notification title";
String notification_des = "This is notification description";
new FCMMessages().sendMessageMulti(MainActivity.this, new JSONArray(user_tokens), notification_title, notification_des, null);