havesource / cordova-plugin-push

Register and receive push notifications
MIT License
150 stars 283 forks source link

Message data must be a one-dimensional array of string(able) keys and values #74

Open nomaam opened 3 years ago

nomaam commented 3 years ago
$data = [
            'title' => $title,
            'body' => $body,
            "ledColor" => [0, 0, 255, 0],
            'actions' => [
                'title' => "test",
                'callback' => 'test',
                'foreground' => "true"
            ]
        ];

$message = CloudMessage::withTarget('token', $deviceToken)
            ->withData($data);

error

Message data must be a one-dimensional array of string(able) keys and values.

How do I include an array of values for "actions" and "ledcolor" as it will only accept a one dimension array?

ahmedawad2 commented 3 years ago

any help here

faugusztin commented 3 years ago

That is a limitation of the Firebase API framework you use. Firebase delivers a map of string keys with string values, and plugin itself decodes the value 'actions' key as JSONArray.

So you need to figure out how to encode an array of objects into a string in language you use on server side, and put the resulting string as the actions parameter.

If the example above is PHP, that would be probably something like this (untested) :

$data = [
            'title' => $title,
            'body' => $body,
            "ledColor" => json_encode([0, 0, 255, 0]),
            'actions' => json_encode([
                'title' => "test",
                'callback' => 'test',
                'foreground' => "true"
            ])
        ];

$message = CloudMessage::withTarget('token', $deviceToken)
            ->withData($data);
ahmedawad2 commented 3 years ago

did json_encode them, and it sounds to be working now with the mobile dev. thanks a lot.