Back

Issue with an Optional Parameter in .createPush()

  • 0
  • Self Hosted
  • Functions
  • Messaging
NeKo
19 Jun, 2024, 00:12

Hello, I can't use the messaging.createPush function in a node.js server function. If I use null for the optional parameters as shown below:

TypeScript
message = await messaging.createPush(
    ID.unique(),            // messageId
    title,                  // title
    body,                   // body
    [],                     // topics (optional)
    users || [],            // users (optional)
    targets || [],          // targets
    data || {},             // data
    null,                   // action (optional)
    null,                   // image (optional)
    null,                   // icon (optional)
    sound,                  // sound (optional)
    null,                   // color (optional)
    null,                   // tag (optional)
    null,                   // badge (optional)
    false,                  // draft (optional)
    null                    // scheduledAt (optional)
);

I get the following error in the Appwrite container log:

2024-06-19 01:44:29 [Error] Method: POST 2024-06-19 01:44:29 [Error] URL: /v1/messaging/messages/push 2024-06-19 01:44:29 [Error] Type: TypeError 2024-06-19 01:44:29 [Error] Message: {closure}(): Argument #8 ($action) must be of type string, null given, called in /usr/src/code/vendor/utopia-php/framework/src/App.php on line 531 2024-06-19 01:44:29 [Error] File: /usr/src/code/app/controllers/api/messaging.php 2024-06-19 01:44:29 [Error] Line: 2876

TL;DR
Issue: Developers are encountering errors when trying to set optional parameters (such as 'action') to null in the `messaging.createPush` function. Changing null to an empty string results in an 'Invalid action param' error. Specifying null for optional parameters causes a 'TypeError' because the parameter must be a string. Solution: The optional parameters cannot be set to null. Instead, use an empty string `''` for optional parameters like 'action', 'image', 'icon', 'color', 'tag', and 'badge' in the `messaging.createPush` function to avoid errors.
NeKo
19 Jun, 2024, 00:15

And if I replace null with an empty string '' like this:

TypeScript
message = await messaging.createPush(
    ID.unique(),            // messageId
    title,                  // title
    body,                   // body
    [],                     // topics (optional)
    users || [],            // users (optional)
    targets || [],          // targets
    data || {},             // data
    '',                     // action (optional)
    '',                     // image (optional)
    '',                     // icon (optional)
    sound,                  // sound (optional)
    '',                     // color (optional)
    '',                     // tag (optional)
    '',                     // badge (optional)
    false,                  // draft (optional)
    ''                      // scheduledAt (optional)
);

Then, I get the following error:

2024-06-19 01:31:10 [Error] Method: POST 2024-06-19 01:31:10 [Error] URL: /v1/messaging/messages/push 2024-06-19 01:31:10 [Error] Type: Utopia\Exception 2024-06-19 01:31:10 [Error] Message: Invalid action param: Value must be a valid string and at least 1 chars and no longer than 256 chars 2024-06-19 01:31:10 [Error] File: /usr/src/code/vendor/utopia-php/framework/src/App.php 2024-06-19 01:31:10 [Error] Line: 774

I see at line 2876 in app/controllers/api/messaging.php the action parameter is a string $action so not null

Thank you for your help

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more