如何在后端通过onTokenRefresh处理Firebase Cloud Messaging [英] How to handle Firebase Cloud Messaging onTokenRefresh on the back end

查看:110
本文介绍了如何在后端通过onTokenRefresh处理Firebase Cloud Messaging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个跨平台应用程序,该应用程序使用Firebase Cloud Messaging来驱动应用程序内聊天功能.有些用户可能会在多个设备上积极使用该应用程序.因此,每当用户设备收到onTokenRefresh触发器时,我们都会将该新注册令牌发送到服务器,以针对该用户进行保存.现在说一个用户已经在服务器数据库中存储了一些注册令牌,我们如何知道这些令牌是用于同一设备,现在应该删除还是用于其他设备,我们应该继续发送给所有这些设备?

We have a cross-platform app that uses Firebase Cloud Messaging to drive an in-app chat feature. Some users might use the app actively on more than one device. So, whenever a user's device receives an onTokenRefresh trigger, we send that new registration token to the server to be saved against the user. Now say a user already has some registration tokens stored in the server database, how will we know if those tokens were for the same device and should now be deleted or if they are for a different device and we should keep sending to all of them?

我已阅读关于设备组消息的文档 ,但对我们的应用程序来说似乎开销太大,而且Firebase服务器不会自动为您从组中删除取代的注册令牌.

I have read the docs on Device Group Messaging, but it looks like too much overhead for our application and it doesn't look like the Firebase server will automatically delete a superseded registration token from the group for you.

如果仅假设记录中的所有用户注册令牌都处于活动状态并发送给所有人,我们是否可以使用响应来决定是否需要在服务器上修剪令牌?

If we simply assume all the user's registration tokens on record are active and send to all, can we use the response to decide if we need to prune a token on the server?

{
    "multicast_id": 6538766984100364080,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1510294979553090%029da28f029da28f"
        }
    ]
}

根据此答案以及针对使用替换令牌的HTTP API进行的一些测试,它看起来不像"success":1结果是一个可靠的指示符,表明不应删除令牌,因为被替换的令牌倾向于继续存在.另外,"success": 0结果可能不是我们可以删除令牌的可靠指标,因为它可能仅表示有效的活动令牌上的临时网络错误.

According to this answer and some tests against the HTTP API with replaced tokens, it doesn't look like the "success":1 result is a reliable indicator that the token should not be removed, because replaced tokens tend to live on. Also, a "success": 0 result might not be a reliable indicator that we can remove the token, because it might just indicate an ad-hoc network error on a valid, active token.

API文档讨论了如何解释结果中的可选registration_id,但是目前尚不清楚这与NotRegistered错误有何不同以及采取最佳措施是什么.

The API documentation talks about how to interpret an optional registration_id in the result, but it is not clear how this differs from a NotRegistered error and what the best action is to take.

任何有关如何处理和管理FCM设备令牌在服务器上到达的见解或最佳实践都将受到赞赏.

Any insight or best practice on how to handle and manage the arrival of a FCM device token on the server will be much appreciated.

推荐答案

我们采用的方法是假设所有onTokenRefresh id是新的,并且将其他设备添加到服务器上的设备列表中.然后,无论何时发送消息,我们都使用返回的结果删除或替换不推荐使用的设备令牌.用PHP实现:

We are going with the approach where we assume all onTokenRefresh ids are new, additional devices that we add to the device list on the server. Then, whenever we send a message we use the returned result to delete or replace deprecated device tokens. Implementation in PHP:

// $devices is a list of the device ids to send to

// 1. send a message to a list of devices
$response = Firebase::request('POST', 'send', ['json' => $this->payloadFor($devices)]);

// 2. check the response to see if we need to make changes to the device list

// if it is a network error, no changes needed
if ($response->getStatusCode() != 200) {
    Log::info("FCM http error " . $response->getStatusCode());
    return;
}

$body = json_decode($response->getBody(), $asArray = true);

// do we need to dig deeper?
if ($body['failure'] == 0 && $body['canonical_ids'] == 0) return;

if (count($body['results']) != count($devices)) {
    Log::info("FCM error : device count not matching result count");
    return;
}

// we have errors that need processing, so step through the results list
foreach ($body['results'] as $key => $result) {

    if (isset($result['error'])) {
        switch ($result['error']) {
            case 'NotRegistered':
            case 'InvalidRegistration':
                $deletedRows = Device::where('token', $devices[$key])->delete();
                Log::info("FCM trimmed: $devices[$key]");
                break;

            default:
                Log::info("FCM error " . $result['error']);
                break;
        }
    }

    // we need to update some device tokens
    if (isset($result['registration_id'])) {
        Device::deprecate($devices[$key], $result['registration_id']);
        Log::info("FCM replaced: " . $devices[$key]);
    }
}

这篇关于如何在后端通过onTokenRefresh处理Firebase Cloud Messaging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆