【问题标题】:FCM Notification - Multiple DevicesFCM 通知 - 多个设备
【发布时间】:2019-07-07 23:14:01
【问题描述】:

我正在尝试为用户拥有多个设备的情况创建一个设备组。我用设备令牌制作了一张地图。 现在我想知道是否可以使用云功能来生成设备组令牌:

然后,监听用户的“app_list”地图上的变化并在 Cloud Functions 上运行类似的东西:

exports.appListAlterado = functions.firestore.document(`usuarios/{idUsuario}`).onUpdate(async (change: any, context: any) => {

    const newValue = change.after.data().app_list;
    console.log("newValue", newValue);
    const regist_ids = [];

    for (const idToken of Object.keys(newValue)) {
        const appToken = newValue[idToken];
        console.log("idToken", idToken);
        console.log("appToken", appToken);
        regist_ids.push(appToken);
    }
    
    console.log("length", regist_ids.length);
    console.log("regist_ids", regist_ids);

});

我尝试直接从 Android 应用程序生成设备组令牌,但在我的测试中,我每次都生成不同的 notification_key(也许我做错了什么)。

检索 notification_key:

public String recuperarChave(String senderId, String userId) throws IOException, JSONException {
        Uri.Builder uriBuilded = new Uri.Builder();
        uriBuilded.scheme("https").appendQueryParameter("notification_key_name", userId)
                .encodedAuthority("fcm.googleapis.com/fcm/notification")
                .build();
        String buildUrl =  uriBuilded.build().toString();
        URL url = new URL( buildUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(false);
        // HTTP request header
        con.setRequestProperty("Authorization", "key=Authorization_Key");
        con.setRequestProperty("project_id", senderId);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accent", "application/json");
        con.setRequestMethod("GET");
        con.connect();
        // Read the response into a string
        InputStream is = con.getInputStream();
        String responseString = new Scanner(is, "UTF-8").useDelimiter("\\A").next();
        is.close();
        // Parse the JSON string and return the notification key
        JSONObject response = new JSONObject(responseString);
        return response.getString("notification_key");
    }

创建设备组:

public String criarGrupo(String senderId, String userId, String registrationId)
            throws IOException, JSONException {
        URL url = new URL("https://fcm.googleapis.com/fcm/notification");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        // HTTP request header
        con.setRequestProperty("Authorization", "key=Authorization_Key");
        con.setRequestProperty("project_id", senderId);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestMethod("POST");
        con.connect();
        // HTTP request
        JSONObject data = new JSONObject();
        data.put("operation", "create");
        data.put("notification_key_name", userId);
        data.put("registration_ids", new JSONArray(Collections.singletonList(registrationId)));
        // Stream
        OutputStream os = con.getOutputStream();
        os.write(data.toString().getBytes(Charset.forName("UTF-8")));
        os.close();
        // Read the response into a string
        InputStream is = con.getInputStream();
        String responseString = new Scanner(is, "UTF-8").useDelimiter("\\A").next();
        is.close();
        // Parse the JSON string and return the notification key
        JSONObject response = new JSONObject(responseString);
        return response.getString("notification_key");
    }

我还考虑将地图与设备的令牌一起存储,并通过它们进行迭代以发送通知。但我想知道这是否是一个好习惯。

所以我的选择是:

1 - 使用 Cloud Functions 监听数据库更改,获取存储的令牌列表并管理设备组的创建,添加设备和删除设备。(在这种情况下如何完成?)。

2 - 尝试从客户端应用程序管理 de Device Group。(在文档中似乎不推荐)。

3 - 迭代令牌列表并为每个令牌发送一条消息。(在这种情况下,它将是来自用户列表的迭代,并且在每个用户中,在用户列表中迭代/令牌地图。

4 - 也许在最后一枪中我可以使用 TOPICS。 /topics/userId 之类的东西,并在用户注销时取消订阅。(在这种情况下,我想知道拥有与用户数量一样多的主题是否会更好。如果我有更具体的主题,它将是一个不好的做法或在未来造成任何问题)。

我想知道哪种方法最适合我的情况。

希望我的问题不难理解。

谢谢!

【问题讨论】:

    标签: android typescript google-cloud-firestore google-cloud-functions


    【解决方案1】:

    我推荐第 3 种解决方案。但是您需要将实时数据库重组为这样。

    fcm_tokens
      - userid
            -device_token1 : fcm token
            -device_token2 : fcm token
    

    因此,当您需要将fcm 发送给您的某个用户时,您只需找到该用户的userId,并遍历fcm_tokens\userid\ 节点

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多