Andrea php Andrea php - 5 months ago 146
PHP Question

Group key onesignal push notification

created a project on one signal.

I can send to php notification on my phone.

The problem comes when it arrives more than one notification. The new notification replaces the previous notification (not yet read ).

How do you get instead say that Android has 2 unread notifications?

I have try write the same

android_group
but the notification never stacked and the newest continue replace the previus.

This is my code:

<?php
function sendMessage(){
$content = array(
"en" => 'text message test'
);

$fields = array(
'app_id' => "XXxxxxXX-xxxxXX-XXxxX-Xxxx-XxxxXXx",
'included_segments' => array('All'),
'data' => array("foo" => "bar"),
'headings' => array("en" => "Test message!!"),
'android_group' => 'TESTGROUP',
'android_group_message' => array("en" => "message"),
'contents' => $content
);

$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);
curl_close($ch);

return $response;
}

$response = sendMessage();
?>


How can i solve this problem?

Answer

android_group used to enable notification stacking only works for Android apps.

If you are you're web push with Chrome for Android the replacement behavior your seeing is expected and isn't configurable. Same behavior on Chrome for Desktop and Firefox.

Also note android_group_message should contain $[notif_count] so the number of unread messages is seen. Example array("en" => "You have $[notif_count] new messages")