Reputation: 840
I want to send push notification to a specific user using PHP. Able to send push notification to all users via OneSignal but unable to send it to specific user. Have created android app using web view As per documentation it requires device id ,but have no idea to fetch via php.
1> Is there any requirement to add another library for push notification for my app. 2> How to fetch Device ID via php required for push notification 3> How to do push notification for specific users
Framework: Laravel App : Android
<?PHP
function sendMessage() {
$content = array(
"en" => 'English Message'
);
$hashes_array = array();
array_push($hashes_array, array(
"id" => "like-button",
"text" => "Like",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
array_push($hashes_array, array(
"id" => "like-button-2",
"text" => "Like2",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
$fields = array(
'app_id' => "my_app_id",
'included_segments' => array(
'Subscribed Users'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'web_buttons' => $hashes_array
);
$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 Auth Key'
));
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();
$return["allresponses"] = $response;
$return = json_encode($return);
$data = json_decode($response, true);
print_r($data);
$id = $data['id'];
print_r($id);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
Send push notification to specific user using Parse
Send Notification to specific user using onesignal
Onesignal documentation https://documentation.onesignal.com/reference/create-notification
Upvotes: 0
Views: 1479
Reputation: 76799
According to their API documentation ...one could send to different segments: EMAIL
, USER TAG
, TEST USERS
. Their sample code hints for filters
(the only option while not storing the tokens):
$fields = [
...
'filters' => [
["field" => "external_user_id" "relation" => "=", "value" => $user->id]
]
];
Their FAQ also features your question:
How do I send a notification to a single user?
In PHP this shouldn't be any different, while it clearly hints for "your database" (Eloquent Model
), which should be able to translate int $user_id
to string
or string[]
$registration_id
aka device registration token. This means that your MessagingService
implementation/s would need to tell Laravel API about their current token - else these would be unknown when sending.
I'd rather give a Java answer to a PHP question:
Use method .setExternalUserId()
to set the Laravel int $user_id
, once it is known to the mobile client. When using external_user_id
as filter criteria, one should also set up identity verification.
Generally one has the option to store tokens and then look them up again
... or to use .setExternalUserId()
, which then can be used as filter criteria.
The mobile client either has to pass it's token - or it's int $user_id
/ string $email
.
Upvotes: 0