How to pass a JSON dictionary as the payload for an IOS app from a PHP-based event handler

Apple supports JSON dictionary for the notification payload so an IOS app can send custom fields, for example. The documentation here says I can send the JSON array shown below in a notification.

{ "aps" : { "alert" : { "title" : "Test Message", "body" : "This is a test message", }, "badge" : 5 },}

How can I send a similar payload from a PHP-based afterCreate() event handler? Neither the “ios-alert” publish option, nor the $msg parameter in the call shown below seem to support this.

Backendless::$Messaging->publish( $msg, $publish_options, $delivery_options )

Thanks for your help!

Headers must be added to the $publish_options argument. You can see the examples on the following page in the docs:

https://backendless.com/documentation/messaging/php/messaging_publish_push_notifications.htm

Hi Mark, thanks for getting back to me. I tried that using setHeaders() call document at https://backendless.com/documentation/messaging/php/messaging_core_classes.htm but it didn’t work. Here’s what I tried.

$message_array = array("aps" => array("alert" => array("title" => $title, "body" => $message), "badge" => 1, "sound" => "bingbong.aiff"));


$publish_options = new PublishOptions();


// Not sure if the next two lines are needed but included them anyway.
$publish_options->putHeader("ios-alert", $child_info[0]['name'] . ' made a purchase.');
$publish_options->putHeader("ios-badge", "1");


// Pass the message array as publish header.
$publish_options->setHeaders($message_array);


$title = "This is the title.";
$status = Backendless::$Messaging->publish( $title, $publish_options, $delivery_options );

I got the following error when my afterCreate() event handler was called.

Backendless API return error: Wrong json format: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: N/A; line: -1, column: -1] (through reference chain: com.backendless.messaging.PublishMessageInfo["headers"] >java.util.LinkedHashMap["aps"]) Error code:5010

Any suggestion is appreciated, thanks!

One small correction to the first section of the code sample aboe–I did set the variables, $title and $message used in the $message_array variable before line #1.

Is it safe to assume that this isn’t supported in the ios-alert, the $message parameter to the publish() call, or any other publish option header?

I believe when you do this, it messes up the internal structure:

/ Pass the message array as publish header.
$publish_options->setHeaders($message_array);

Headers should be set only with the putHeader method.

I am sorry, I am having hard time understanding the question. Could you perhaps rephrase it?)

OK, will try that.

Our responses crossed, looks like. I was asking if my original question of passing IOS-specific JSON (or an associative array like you suggest) is supported or not.

I will try the setHeader() call, thanks.

Hi Mark, I tried your suggestion as shown below.

$title = "This is the title.";
$message = "This is the message.";
$message_array = array("aps" => array("alert" => array("title" => $title, "body" => $message), "badge" => 1, "sound" => "bingbong.aiff"));
$publish_options->putHeader("ios-alert", $message_array);

I got the following error message, pretty much the same as the one I reported earlier.

Backendless API return error: Wrong json format: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: N/A; line: -1, column: -1] (through reference chain: com.backendless.messaging.PublishMessageInfo["headers"]->java.util.LinkedHashMap["ios-alert"]) Error code:5010

Hi Sundar,

You cannot use the example JSON from Apple docs directly, instead you can use separate headers like “ios-alers”, “ios-badge”, “ios-sound” (the names are provided in the doc - https://backendless.com/documentation/messaging/php/messaging_publish_push_notifications.htm )

And set them not as a PHP array, but like a dictionary. Example from the same docs:

$publish_options = new PublishOptions();
$publish_options->putHeader( "android-ticker-text", "You just got a push notification!" )
                ->putHeader( "android-content-title", "This is a notification title" )
                ->putHeader( "android-content-text", "Push Notifications are cool" );
  
$status = Backendless::$Messaging->publish( "Hi Devices!", $publish_options, $delivery_options );

(just change the appropriate android headers to ios ones)

Thanks for getting back to me Sergey. In that case, it would be something like this?

$publish_options->putHeader("ios-title", "This is the title");
$publish_options->putHeader("ios-body", "This is the body");
$publish_options->putHeader("ios-custom-parameter", "This is a custom parameter");

The Backendless documentation page you shared above doesn’t include all possible remote notification options Apple supports, hope you can help clarify–does Backendless allow me to use all APS dictionary keys, and alert keys documented here by Apple?

Also, Apple remote notification also allows custom key-value pairs in the payload, does Backendless pass them through as well?

Currently only the documented three options are officially supported: “ios-alert”, “ios-badge” and “ios-sound”. In the example payload below they will be placed into “aps->alert”, “aps->badge” and “aps->sound” parameters respectively. You cannot modify anything else inside the “aps” dictionary for now, but we shall do some improvements here in the future.
Also, you may set any number of additional parameters and they’ll be placed below like “acme1” and “acme2” properties.

{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}

Thanks for clarifying. It would be great if the documentation reflected the limited support for the APS dictionary :).

And of course, consider this a feature request to support passing the full APS dictionary, thank you!