【问题标题】:How to send notification to specific users with FCM?如何使用 FCM 向特定用户发送通知?
【发布时间】:2016-10-08 15:11:22
【问题描述】:

我为 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 ...]"
         }

我该怎么做? 我需要创建一个网络服务器或其他东西吗?

【问题讨论】:

    标签: android notifications firebase google-cloud-messaging firebase-cloud-messaging


    【解决方案1】:

    我需要创建一个网络服务器吗

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

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

    将推送发送到“qrgqry34562456”和“245346236ef”。

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

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

    【讨论】:

    • 感谢您的回复。这对我很有帮助。我阅读了 FCM 的几乎所有文件。有与您的答案相同的内容。我想确定。因为我认为有一个通知服务器。
    • registration_id 和注册令牌是一样的吗?
    • @AnggaAriWijaya 是的
    • 好,但你可以从 FirebaseInstanceId.getInstance().getToken() 或类 AppFirebaseMessageingService: FirebaseMessagingService() { override fun onNewToken(token: String?) { super.onNewToken(token) }覆盖乐趣 onMessageReceived(msg: RemoteMessage?) { super.onMessageReceived(msg)}}
    • @TheWebGuy 是的。如果你不能告诉 Firebase 消息是谁,你将如何告诉他们向谁发送消息?
    【解决方案2】:

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

    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";
            }
    

    【讨论】:

    • 不推荐这种方式,因为任何人都可以使用 Smali2Java 等工具获取您的 API 密钥,并且可以将通知发送给任何人。
    • 这是黑客攻击的一部分,是的,我也不推荐这样做,但我给出的答案是我们也可以从这里进行操作,如果需要,我们可以通过加密密钥来保护该部分。是的,谢谢你让我知道这一点。
    • 我们将如何使用它?
    • 我们可以通过加密密钥来保护该部分 - 这不是真的
    • 不要这样做。您的私有 api 密钥供您在您控制的硬件上使用。不要公开分发。
    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2021-03-03
    • 2021-01-14
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多