7.4 C
New York
Wednesday, December 18, 2024

android – How does whatsapp preserve machine synced with the database


I used to be constructing a chat app. I had this drawback with how am I going to maintain the native knowledge updated with the information on my laravel server’s DB. I considered Whatsapp and the way they preserve their app’s native DB at all times synced and the one factor I might consider is the notifications. Every (message, motion, … and many others) pushes a notification to the involved customers’ gadgets, both a silent notification like deleting a message or a brand new message which might push a visual notification. I attempted replicating this impact in Android however had some issues like notifications collapsing, firebase working in a special isolate, and so forth, however now it really works on Android. After all, I wanted so as to add a periodic work supervisor’s process to ship a syncing request to the server to make sure the machine is synced as I can not rely solely on notifications. On Android, all knowledge messages are delivered in each machine state attainable (foreground, background, terminated, machine locked, reconnecting to the web). The issue is with iOS as silent knowledge messages are being throttled. I can not ship a number of knowledge messages to iOS and count on all of them to be obtained. I attempted rethinking my technique however I could not come to a solution. Can somebody assist me how can I obtain such a mechanism ?

That is my code for sending a silent notification to my flutter app and I can then push a neighborhood notification conditionally:

public operate sendDataMessage($deviceTokens, $knowledge = [], $precedence = "excessive")
{
    $messaging = app('firebase.messaging');
    $clean_data = $knowledge;
    if (isset($knowledge["type"]) && $knowledge["type"] == "chat" && $knowledge["action"] == "receiveMessage") {
        unset($clean_data["fcm_message"]);
    }
    $messages = [];
    foreach ($deviceTokens as $token) {
        attempt {
            if (isset($knowledge["type"]) && $knowledge["type"] == "chat" && $knowledge["action"] == "receiveMessage") {
                $chat_message = $knowledge["fcm_message"];
                $clean_data["title"] = Chats::choose("title")->discover($knowledge["chat_id"])->title;
                $clean_data["body"] = $chat_message["sender"]["name"] . ": " . ($chat_message["content"]["type"] == "Textual content" ? $chat_message["content"]["body"] : $chat_message["content"]["type"]);
            }
            $messages[] = CloudMessage::withTarget('token', $token)
                ->withData($clean_data)
                ->withAndroidConfig(AndroidConfig::fromArray([
                    'priority' => 'high',  // Use high priority
                    'ttl' => 86400 * 28, // 4 weeks in seconds
                ]))
                ->withApnsConfig(ApnsConfig::fromArray([
                    'headers' => [
                        // 'apns-collapse-id' => isset($data["type"]) && $knowledge["type"] == "chat" && $knowledge["action"] == "receiveMessage" ? strval($chat_message["id"]) : "1",
                        'apns-expiration' => strval(time() + 86400 * 28), // Expiry timestamp
                        'apns-priority' => '5',
                    ],
                    'payload' => [
                        'aps' => [
                            // "alert" => [
                            //     "title" => "New Message",
                            //     "body" => "Message from John"
                            // ],
                            // "sound" => "default",
                            "content-available" => 1,
                            "mutable-content" => 1
                        ],
                    ],
                ]));
        } catch (Exception $e) {
            return $e;
        }
    }
    attempt {
        $report = $messaging->sendAll($messages);
    } catch (NotFound $e) {
        return $e;
    } catch (Exception $e) {
        return $e;
    }

    return $report->successes()->depend();
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles