11

我准备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服务器或其他东西?

回答

14

难道我需要创建一个Web服务器

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

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

    } 
} 

将所述推送发送至“qrgqry34562456”和“245346236ef”。

您在调用中使用的注册ID是被称为“令牌”在这个回调在应用程序中的一个。

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

感谢您的答复。这对我有帮助。我几乎阅读了FCM的所有文档。其中有同样的东西,就像你的答案一样。我想确定。因为我认为有通知服务器。 –

+1

是registration_id和注册标记相同的东西? –

+1

@AnggaAriWijaya是 –

0

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

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

       final String apiKey = "AIzaSyBsY_tfCQjUSWEWuNNwQdC1Vtb0szzz"; 
       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"; 
     } 
+0

这是不推荐的方式,因为任何人都可以通过使用像Smali2Java工具让你的API密钥,并可以发送通知任何人。 – kunwar97

+0

这是黑客攻击的一部分,是的,我也并不推荐这样做,但我给,我们可以从这里也做了回答,我们可以通过,如果需要加密密钥的安全部分。是的,谢谢让我知道这一点。 –

+0

我们将如何使用它? –

相关问题