繁体   English   中英

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

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

我为FCM准备了接收器,可以向所有设备发送通知。

带有此链接的gcm-http.googleapis.com/gcm/send可以发送给已注册的目标用户并将其发布到目标设备,如下面的json:

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

但是,我需要通过电子邮件或姓名等向我选择的目标用户发送通知。 例如:

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

我怎样才能做到这一点? 我是否需要创建Web服务器或其他东西?

我需要创建一个Web服务器吗?

是。 您需要一个可以将姓名/电子邮件映射到注册ID的地方。 例如,这些注册ID必须包含在FCM的请求中

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

    }
}

将推送发送到'qrgqry34562456'和'245346236ef'。

您在呼叫中使用的注册ID是应用程序中此回调中称为“令牌”的ID。

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

您可以使用此代码将消息发送到其他设备。 此代码中不需要服务器。

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