Reputation: 1637
I'm using onesignal first time. I have already spent too much time on R&D. But output is almost zero. So here I go with my requirements.
I have successfully developed onesignal demo. But it is user independent. If I trigger notification, all the device gets notification. So here are my questions.
If anyone can show me basic example, it will be appreciated. I'm from PHP side. It will be good for me if I get answer from PHP developer's view. I have already asked this question on stackoverflow. But answer was from Android view. But I think he doesn't know that on server side, we have to manage all the devices. So I haven't get much more from that answer.
Upvotes: 0
Views: 7133
Reputation: 309
$to - Device ID
$title - Notification Title
$message - Notification Message
$img - Full image link
Usage:
With Demo Values :
function sendnotification($to, $title, $message, $img)
{
$msg = $message;
$content = array(
"en" => $msg
);
$headings = array(
"en" => $title
);
if ($img == '') {
$fields = array(
'app_id' => 'YOUR_APP_ID',
"headings" => $headings,
'include_player_ids' => array($to),
'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
'content_available' => true,
'contents' => $content
);
} else {
$ios_img = array(
"id1" => $img
);
$fields = array(
'app_id' => 'YOUR_APP_ID',
"headings" => $headings,
'include_player_ids' => array($to),
'contents' => $content,
"big_picture" => $img,
'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
'content_available' => true,
"ios_attachments" => $ios_img
);
}
$headers = array(
'Authorization: key=**APP_KEY**',
'Content-Type: application/json; charset=utf-8'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
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);
return $result;
}
Upvotes: 0
Reputation: 499
Onesignal is a good platform to send push notifications to different types of devices. I'm using the same in my project. What I'm doing is that, whenever the user is logged in, I'll get the device token from the client and create a user in onesignal using their API. (I believe you know how to configure the onesignal with google project number & google API key for android, p12 files for ios, etc...)
Android Perspective:
So, for the same google API key, onesignal will always return the same player id, even though you delete and create the user again. In a big project, developers should expect that a user might be logged in to different devices (android, ios, etc). So a user can have multiple tokens, and so is the player id. One more requirement which you've is that the user doesn't want to get notified if he's not logged in.
So keeping all of your requirements in mind, I can suggest you one MySQL table structure to store the user details.
One table to store the device type (optional)
-id
-device_type
One to store the tokens and player ids
-token_id
-user_id
-device_id
-device_token
-onesignal_player_id
-subscription
These two tables are needed for onesignal to work.
Case 1:
Whenever a user try for logging in, first check the token table. Find if there is any other user having the same given token (which means another user logged in with the same device). Delete such token if there is any. Then create a new player in onesignal and save the details into the token table. Mark subscription as 1, which is subscribed for notification. Whenever the user logs out, mark the subscription to 0, which is unsubscribed for notification.
Case 2:
Whenever the user logs in, check for the token whether it is already present in the table. If it is yes and the owner of the token is the same, then just mark the subscription as 1. So that further notifications will be received by him.
Case 3:
When a user uses another device to login (multiple devices at a time), the gcm token will be different for sure. So save that as another token for the same user and mark the subscription as 1. So, both devices will get the notification.
In the end, before sending the notifications, fetch all tokens of that particular user which is subscribed and send the notification to all at a single shot. That will fix your problem.
I know this might be a bit confusing. But don't worry, we can fix it.
Upvotes: 6
Reputation: 3337
Upvotes: -1