简体   繁体   English

如何使用FCM向特定用户发送通知?

[英]How to send notification to specific users with FCM?

I prepared the receiver for FCM and can send a notification to all devices. 我为FCM准备了接收器,可以向所有设备发送通知。

gcm-http.googleapis.com/gcm/send with this link can send to target users who is registered and post to the target devices like below json : 带有此链接的gcm-http.googleapis.com/gcm/send可以发送给已注册的目标用户并将其发布到目标设备,如下面的json:

 {
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[registration id]"
         }

However, I need to send notifications to target users which I choose, via email or name...etc . 但是,我需要通过电子邮件或姓名等向我选择的目标用户发送通知。 For example: 例如:

{
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[email or name or sex ...]"
         }

How can I do that? 我怎样才能做到这一点? Do I need to create a web server or something else? 我是否需要创建Web服务器或其他东西?

Did I need to create a web server 我需要创建一个Web服务器吗?

Yes. 是。 You need a place where you can map name/email to registration IDs. 您需要一个可以将姓名/电子邮件映射到注册ID的地方。 These registration IDs must be included in the request to FCM, for example 例如,这些注册ID必须包含在FCM的请求中

{
    'registration_ids': ['qrgqry34562456', '245346236ef'],
    'notification': {
        'body': '',
        'title': ''
    },
    'data': {

    }
}

will send the push to 'qrgqry34562456' and '245346236ef'. 将推送发送到'qrgqry34562456'和'245346236ef'。

The registration ID you use in the call is the one that's called 'token' in this callback in the app. 您在呼叫中使用的注册ID是应用程序中此回调中称为“令牌”的ID。

public class MyService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
    }
}

you can send message to other device using this code. 您可以使用此代码将消息发送到其他设备。 there is no need of server in this code. 此代码中不需要服务器。

public  String send(String to,  String body) {
            try {

                final String apiKey = "AIzaSyBsY_tfxxxxxxxxxxxxxxx";
                URL url = new URL("https://fcm.googleapis.com/fcm/send");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Authorization", "key=" + apiKey);
                conn.setDoOutput(true);
                JSONObject message = new JSONObject();
                message.put("to", to);
                message.put("priority", "high");

                JSONObject notification = new JSONObject();
               // notification.put("title", title);
                notification.put("body", body);
                message.put("data", notification);
                OutputStream os = conn.getOutputStream();
                os.write(message.toString().getBytes());
                os.flush();
                os.close();

                int responseCode = conn.getResponseCode();
                System.out.println("\nSending 'POST' request to URL : " + url);
                System.out.println("Post parameters : " + message.toString());
                System.out.println("Response Code : " + responseCode);
                System.out.println("Response Code : " + conn.getResponseMessage());

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // print result
                System.out.println(response.toString());
                return response.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM