OneSignal push notification sending to all user instead of one player - php

I am trying to send push notification to one device by its player_id stored in my database.
but whine it try it keep sending to all users
i don't know what is wrong in my code
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXX',
'include_player_ids ' => $player_ids,
'included_segments' => array(
'All'
),
'data' => $data,
'headings' => $title,
'contents' => $content,
'web_buttons' => []
);
$fields = json_encode($fields);
if($player_ids) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
));
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);

just remove 'included_segments' => array('All'),
If you fill both of included_segments and include_player_ids, you will send to all selected player ids and also all selected segments. Segment All mean all subscribers.
you can read about other option on official documentation

I need to remove included_segments and web_buttons
both of them not just included_segments , when I try to just remove included_segments it give me error of {"errors":["You must include which players, segments, or tags you wish to send this notification to."]}
also I don't need 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'.

Related

Separate Notifications do not appear on a same device subscribed to a topic

when i push multiple notifications in a loop via api subscribed to topic using FCM, the previous notification replaced by new notification on my android device. Is there a way to push multiple notifications separately. I am using PHP Curl request.
foreach ($result as $item)
{
$message = array
(
'body' => "Details:".$item['tagid'],
'title' => "Tag:".$item['event'],
'key1' => "NotificationActivity"
);
$fields = array(
'to' => '/topics/all',
'data' => array
(
'message' => $message
),
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
There was problem on my Firebase service. Not identifying notification on id basis.
Problem was, I was passing 0 in id field.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
After setting unique id of each notification, it's working:
notificationManager.notify(id /* ID of notification */, notificationBuilder.build());

Receive Push Notification Twice firebase PHP Script

I'm new to Firebase Send Push Notification. I'm working on Send Push notification To multiple Users Using PHP Script.
PHP SCRIPT
function sendPushNotification($fb_key_array, $title) {
$finalPostArray = array('registration_ids' => $fb_key_array,
'notification' => array(
'title' => $title,
'sound' => 'default',
'badge' => '1'
),
'priority' => 'high',
);
$server_key = env("SERVER_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($finalPostArray)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $server_key));
$server_output = curl_exec($ch);
print_r($server_output);
return $server_output;
curl_close($ch);
}
I'm send notification using Queue Jobs on Particular Time.
I have a problem when I send Three notification at same time than Notification receive Twice.
Response From This Curl
App\Jobs\PushNotification
<pre>{"multicast_id":4668247729883149905,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386021446151%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][261] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:01][262] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":4539206321688711164,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386022124068%e609af1cf9fd7ecd"},{"message_id":"0:1616386022123922%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][262] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:02][263] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":1591082090876608595,"success":3,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386023195991%e609af1cf9fd7ecd"},{"message_id":"0:1616386023196003%e609af1cf9fd7ecd"},{"message_id":"0:1616386023195724%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:02][263] Processed: App\Jobs\PushNotification
And Receive 6 Notification .but I send Only Three Notification. Where is my Fault ?
Help Thank You
I am not so sure about that there is nothing wrong with your func but you can try this it's the same but little different if not work so that mean you have a loop somewhere in your code ..
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $target,
// fix new json format
'notification' => array(
'body' => 'this is firebase test mesging',
'title' => 'hello from firebase',
'badge' => 1,
'sound' => 'default',
'priority' => 'high',
)
);
$fields = json_encode($fields);
$server_key = 'ur_server_kay';
// inherited key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
var_dump($result);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

Sending push notification to specific user by tag in OneSignal

I am new to Onesignal, and I am testing to send push notifications to my app users.
All users are included into OneSignal segments and I am using these segments to send push notifications to all users included in the needed segment.
But now I need to send a push notification to a specific user. Each user has a tag called id_usuario and a value for it.
This is how am I sending the push notifications to all users from a segment:
$content = array(
"en" => 'El usuario '.$username.' ha creado el spot '.$nombre_spot
);
$fields = array(
'app_id' => "....",
'included_segments' => array($segmento), //,array('AND'),array('App users'),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'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; charset=utf-8',
'Authorization: Basic ...'));
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);
What should I change to send the push notification only to a user based on his id?
Onesignal tag is "id_usuario" and the value will be saved in variable $id_user.

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?
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")

Pushbullet no notification on smartphone using API

I created a little app which uses Pushbullet's API.
It is writtent in PhP and uses cURL to proceed the different requests.
At first, I use oauth2 to recieve an access token.
Then it is /v2/pushes/ to send a push to the registred account.
The message is recieved on the Pusbullet account but nothing shows up on the smartphone. There is no notification.
I hope someone can help me.
Thanks.
My function which sends the push:
function send_push($token){
$data = array(
'type' => 'note',
'title' => 'Alert',
'body' => 'My body :)!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.pushbullet.com/v2/pushes');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token
));
$data = curl_exec($ch);
curl_close($ch);
}

Categories