Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be the appropriate system flow for functionality of the push-notification service that is developed using PHP and Pushwoosh?

I'm a PHP Developer by profession. So, I don't have much knowledge about mobile apps, iOS, Android, etc. such like things. So, please try to understand me.

I've an app developed using iOS by the respective development team. Now, I have to provide this app the Push-Notifications using Pushwoosh.

I've understood what is mean by push-notification and usage of Pushwoosh for the same. Also, I'm able to send sample push-notification by using the app_id and auth_key I got from Pushwoosh Control Panel. When I run the corresponding PHP file from the server the 'Success Message' with proper status code I receive.

My query is about actually how to implement this push-notifications service into the app from server side perspective?

Now for this let's consider the more dynamic and practical thing.

Actually there is one table in MySQL database which contains the notifications. As soon as any new entry gets inserted into that table I've to check whether it(notification) is for logged-in user or not. If there is/are any new notifications generated for logged in user I've to send the push-notification to the concerned user to his/her respective device through 'Pushwoosh'.

Now my question is should the PHP file which contains the code for checking the new notifications and sending it as a push-notifications be called from the app side or is there any other way around?

In other words my doubt is should this check for new notifications should be performed only upon a request coming from app?

I discussed the same issue with the mobile app development team, they told me we don't send you any kind of request you simply have to send us the push-notification. They told me the meaning of push-notification is only the same that app never send any request to the server, the server itself should send the notification to the app whenever available.

Then in this case how should the PHP file that contains the code should get execute since there is no request received for it?

Another question here is if the app is not going to send any request to the PHP file then how should I know which user is logged in and requesting for the new notifications generated for him/her, if any?

Suppose, if the request has come for the PHP file then should I need to make token based authentication in PHP code for that particular user or it will be done on app side and only upon validating the user successfully they will send the request to PHP file?

Also the check for new notifications should be make for every two minutes interval(polling). From where this check should be made? I mean will the PHP file receive request from app for every two minutes or what?

Please help me resolving these tedious issues.

Thanks.

Following is my sample code (The Auth Token and App ID have been changed for security purpose):

<?php
  define('PW_AUTH', 'XXXXXXXXXXX');
  define('PW_APPLICATION', 'XXXXXXXXXXX');
  define('PW_DEBUG', true);

  function pwCall($method, $data) {
    $url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
    $request = json_encode(['request' => $data]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

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

    if (defined('PW_DEBUG') && PW_DEBUG) {
      print "[PW] request: $request\n";
      print "[PW] response: $response\n";
      print "[PW] info: " . print_r($info, true);
    }
  }

  pwCall('createMessage', array(
    'application' => PW_APPLICATION,
    'auth' => PW_AUTH,
    'notifications' => array(
            array(
                'send_date' => 'now',
                'content' => 'test',
                'data' => array('custom' => 'json data'),
                'link' => 'http://pushwoosh.com/'
            )
        )
    )
  );  
?>

In the above code only I'm going to integrate code for checking and sending new notifications if available.

like image 973
PHPLover Avatar asked Jun 04 '15 03:06

PHPLover


People also ask

How do you implement a push notification service?

Open your app on other browsers or devices and try subscribing them to push notifications and then clicking the Notify all button. You should receive the same notification on all of your subscribed devices (i.e. the ID in the body of the push notification should be the same).

How does a push notification system work?

Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app. All the mobile platforms – iOS, Android, Fire OS, Windows and BlackBerry – have their own services for supporting push.

What is needed for push notifications?

To be able to receive push notifications, the app must be configured and registered with a Push Notification Service Provider. The most widely used service provider globally is GCM (Google Cloud Messaging), or FCM (Firebase Cloud Messaging), a Google-run entity.


2 Answers

Lets begin with the installation of the iOS app. The iOS team should take care to register the device after the installation to the Apple Push Notification Server (APNS) and get a push token.

On login they should send this push token to the PHP server and the server should take care to store it along with the user's data. Typically (but not obligatory) after a successful login the server should generate some unique token and send it back to the device which will be used afterwards for the communication between them. If there are no expiration requirements, this token is good enough to know that the user is logged in.

If an expiration is required than the server should save a time stamp of the moment when the token is generated and store it to the DB. This also requires to update the time stamp whenever the user interacts with the app / the server.

A cron job could be set to run on every 2 minutes which will call a PHP script. This PHP script goes through the table in the DB and checks for new notifications and if it finds such, should try to send a push notification. If a 'is logged in' check is required, the script should check the user's token and on success should send a /createMessage API request to Pushwoosh with the corresponding device push token. The parameter in the JSON request should look similar to this:

"devices":["dec301908b9ba8df85e57a58e40f96f523f4c2068674f5fe2ba25cdc250a2a41"]

Note that you can add here up to 1000 push tokens.

This way you will send a push notification to the concrete user's device.

Please note here, that 1 user could install the app on multiple devices. In this case all push tokens should get stored and used when sending the push notification.

I hope I didn't miss something.

like image 146
M. Sabev Avatar answered Oct 17 '22 04:10

M. Sabev


Push notifications are often used to bring users into an app when they haven't logged in for awhile or when there is some event or promotion going on. Because of that most push notification implementations cannot rely on a request from the app itself.

In this case a new entry in a MySQL database is the condition for sending out a push notification, so I would create a PHP file which would be run on a regular schedule to check for new, unsent rows in the MySQL database. The best way to request PHP scripts on a schedule is to set up a cron job on your server to run every two minutes. There's a lot of information about setting up cron jobs on the internet, and most ops teams can do it for you. But if you need/want to set it up yourself, the basic steps are:

  1. SSH in to the server that will be running the script
  2. Run crontab -e to edit the cron config file
  3. Add the cron command. It would like something like this:

    */2 * * * * cd /absolute/script/path && php script_name.php
    
  4. Save and exit the file

The */2 means it will run every 2 minutes, the rest of the * characters mean the script will run every hour, day, month, and day-of-week. Lastly is the command line command to execute. Changing directories before running the PHP file will give you the same current working directory behavior as requesting the PHP file from a webserver.

There are a number of ways to identify if users are logged in. One I would suggest is have a field in the DB that would be updated with logged in status and time of the last action that user made while logged in. The time of last action is useful as users often don't log out of apps and instead just close them. You could use the time since their last action as a way to see if the app has been inactive long enough to assume that the user was no longer using it. This logged in field could also be stored in a caching layer if your application uses one to reduce DB load.

In terms of token authentication, that would have to come from the app side. iOS devices require that the user give permission to the app before a push notification token is generated and sent to the app itself. This is something that the app development team should have taken care of, and they should be able to provide you with a location these tokens are stored, or at least help you to create a storage solution for the tokens. If you store the push notification token and the associated user id in a MySQL DB it should just be a simple lookup to access their authentication token.

To summarize the above information, I would create a solution similar to the following:

App Side:

  1. User installs app, agrees to recieve push notifications
  2. A push notification auth token is created, store this in a DB associated with the user

PHP Side:

  1. At some point, a push notification gets added to the MySQL database and needs to be sent out
  2. The cron job runs your PHP script every two minutes
  3. The PHP script checks the DB for rows that haven't been sent (a simple boolean column should suffice for this)
  4. Check the DB for the user's logged in status. If it's set to logged in, check to make sure their last action wasn't too long ago
  5. For each unsent push notification with a user that has an appropriate logged in status, look up the authentication token for the user in the DB
  6. Send the authentication token and the push notification message off the Pushwoosh to handle the delivery of the message
  7. Update the push notification rows in the DB as being sent
like image 28
Glen Avatar answered Oct 17 '22 04:10

Glen