0

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 a website. iOS app of the site and Android app too. Just like whatsapp or facebook.
  • When user logged in any of the above, user should get webpush notification or iOS notification, based on logged in device.
  • If user is logged in website as well as iOS app at a time, user should get notification on both.
  • If user gets logged out from iOS, user should not get notification in iOS device till he login again.
  • If another user login in to same iOS app, he should not get notifications of last logged in user. He should get only notifications related to him.
  • Same for website too.

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.

  • I have worked on GCM (I'm from PHP background). So as per my knowledge, push notification works on device id. So should I store multiple device id for a single user in my mysql database (for chrome, iOS, android etc)?
  • How will I get that device id? Remember, if user is not logged in, I don't want to send notification.
  • How to store it in my Database? I will pass that device id to my server by calling API and my server will store it in database. But when I should send? Because If user is not logged in, then how can I come to know that for which user I have to save device id?
  • How to check if user is changed on device or is same or is logged out?

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.

Community
  • 1
  • 1
Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60
  • I'm using Onesignal in my product and I know the workaround for PHP also. So trust me. May I know who is initiating the push notifications (backend-php api or directly from onesignal dashboard) and in which scenarios? – JTheDev Mar 10 '17 at 10:34
  • 1
    @JTheDev. backend-php api is initiationg push notifications – Akshay Vaghasiya Mar 10 '17 at 11:00
  • 1
    @JTheDev. What do you mean by which scenarious? Let me explain one notification trigger event. When some one comment on post, user who have posted post should receive notification. And I think you are from laravel, then for your better understanding, I'm using laravel 5.3 notification (https://github.com/laravel-notification-channels/onesignal). My headache is `ONE_SIGNAL_PLAYER_ID`. How to obtain and maintain it. I don't know even how it works. – Akshay Vaghasiya Mar 10 '17 at 11:26

3 Answers3

6

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:

  • We have to keep one thing in our mind that, a device's gcm token will always remains the same, as long as the google project number and google API key is not changed.
  • If the same project is having a different application like b2b, b2c, etc... (with same DB and backend), your google API key will change, so is the gcm token and so is the onesignal player id.

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.

  1. One table to store the device type (optional)

      -id
      -device_type
    
  2. 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.

enter image description here

JTheDev
  • 499
  • 1
  • 8
  • 18
  • Thanks for your effort. But I don't know how to configure the onesignal with google project number & google API key for android, p12 files for ios, etc.. – Akshay Vaghasiya Mar 10 '17 at 12:13
  • And how will I get device token when user is logged in website (chrome, firefox etc)? – Akshay Vaghasiya Mar 10 '17 at 12:14
  • Android developers know how to generate gcm tokens of a device. It depends on the google API key which they're used in that project. Google project number you'll get from google developer console. Copy both. Open the onesignal dashboard. Select your application if you've created. otherwise, create one. In app settings, platform tab is there. Inside that, click configure button nearby Google Android section. – JTheDev Mar 10 '17 at 12:21
  • Okay. I come to know that I manually have to manage either user is logged in or logged out in so and so device by setting `subscription` field of table (Case 2). So onesignal doesn't automate to stop notification for logged out user. Am I right? – Akshay Vaghasiya Mar 10 '17 at 12:22
  • I think you are focusing more on Android. Can we please proceed by considering [web push](http://prntscr.com/ei8lp3) as client rather than android? – Akshay Vaghasiya Mar 10 '17 at 12:25
  • Yes, they won't do that. You've to prevent it before sending. – JTheDev Mar 10 '17 at 12:26
  • No idea about the web push notification, but the theory must be the same and so is the table structure – JTheDev Mar 10 '17 at 12:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137757/discussion-between-akshay-vaghasiya-and-jthedev). – Akshay Vaghasiya Mar 10 '17 at 12:30
0

$to - Device ID

$title - Notification Title

$message - Notification Message

$img - Full image link

Usage:

sendnotification($to, $title, $message, $img);

With Demo Values :

sendnotification("Device_ID","Test Notification","Test Message","https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png");

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;
}
Shreyas Kheni
  • 187
  • 2
  • 11
-1
  • For store multiple device for single user you must identify them on some way. You can turn on web hooks and when new user subscribed, you can send his identify information (id, email etc). With this data you'll get his new device id. Then you must store it in way what you like.
  • To tracking is user login or not, you can use identify information: send request to API when it login and logout. Notice that user can be logged in on one device and logged out on other.
  • That's why I would be also send device id and store logged status in pair, but you can do it in way what you like.
Pavlo Zhukov
  • 3,007
  • 3
  • 26
  • 43