Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用FCM向特定用户发送通知?_Android_Notifications_Firebase_Google Cloud Messaging_Firebase Cloud Messaging - Fatal编程技术网

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

Android 如何使用FCM向特定用户发送通知?,android,notifications,firebase,google-cloud-messaging,firebase-cloud-messaging,Android,Notifications,Firebase,Google Cloud Messaging,Firebase Cloud Messaging,我为FCM准备了接收器,可以向所有设备发送通知 gcm http.googleapis.com/gcm/send通过此链接可以发送给已注册的目标用户,并发送到目标设备,如下所示: { "notification": { "title": "sample Title", "text": "sample text" }, "to" : "[registration id]" }

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

gcm http.googleapis.com/gcm/send通过此链接可以发送给已注册的目标用户,并发送到目标设备,如下所示:

 {
     "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在应用程序的此回调中称为“令牌”

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

谢谢你的回复。这对我很有帮助。我几乎读了FCM的所有文件,都和你的答案一样。我想确定一下。因为我认为有一个服务器用于通知。注册id和注册令牌是一样的吗?@AnggaAriWijaya是的,但是您可以从FirebaseInstanceId.getInstance().getToken()或类AppFirebaseMessageService:FirebaseMessagingService(){override fun onNewToken(令牌:String?{super.onNewToken)获取id(令牌)}onMessageReceived(msg:RemoteMessage?{super.onMessageReceived(msg)}@WebGuy是的。如果你不能告诉firebase消息是谁,你会如何告诉firebase向谁发送消息?这不是推荐的方法,因为任何人都可以通过使用Smali2Java之类的工具获取API密钥,并可以向任何人发送通知。这是黑客攻击的一部分,是的,我也不建议这样做,但我给出的答案是,我们可以在rom也在这里,如果需要,我们可以通过加密密钥来保护这一部分。是的,谢谢你让我知道这一点。我们将如何使用它?我们可以通过加密密钥来保护这一部分-这不是真的。不要这样做。你的私有api密钥供你在控制下的硬件上使用。不要公开分发它。