In the last post of this series I gave an introduction about how one could send and receive push notifications in Windows Phone 8. In this post I am going to discuss toast notifications. There are three parts to this tutorial.
- Sending a push notification
- Setting up a client to receive toast notifications
- Receiving toast notifications
Sending a push notification
We need a webpage that sends a toast notification by using the URI that is returned when the push channel is created on the device. The MSDN website explains how this can be done using ASP.NET but in this tutorial I will be using PHP to setup a push notification system on a server. You will be needing a domain and hosting to setup your notification service on the server. If you don’t have any website then sign up for free on 000webhost.com and get free domain and hosting. They provide hosting absolutely free, there is no catch. You get 1500 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is amazing and easy to use website builder. Moreover, there is no any kind of advertising on your pages.
Follow these steps to setup a service
Step 1. Add a database table to store the URIs
You will need a database table. From the cpanel of your hosting account add a new MYSQL database and assign a user with all privileges to it. I use a godaddy account but your cpanel will also have similar options.
Once you create a database, add a new/existing user who can manage that database.
Now open phpmyadmin from cpanel and login. You will need to create a new table in the database `windowsapptutorials`. My table’s structure is shown below for reference.
Step 2. Add a file in file manager to connect to this database
Go to your file manager using cpanel and browse to public_html folder in it. Create a new directory in it and add a new file to it. Name the file config.php. This script will be used to connect to your database.
Step 3. Add a file to register new URIs
Our client app will send us a request with a notification URI (we call it GCM id) and we need to save it in our databases so that we can send notifications to it later. Create a file userid.php which will take a $_GET request containing the URI and add it to the database. It will also return the user id to be used by the client for updating URI for a device.
- The script queries the database to get the last inserted user id which is of the form user<id_no> and finds its substring to get <id_no>
- It increments the <id_no> and gets an encoded gcm id (URI) from the URL.
- It then inserts a new record in the database for the incremented user id along with the gcm id provided
- Finally it echoes the incremented userid which the client app saves locally in its isolated storage.
Step 4. Add a file to update existing URIs
The notification URI keeps changing every few days and you will need to update the existing URIs to continue sending messages to the client app. For this purpose let us create another file updategcm.php which will take $_GET parameters userid and new gcm id (new URI) and update the URI corresponding to that userid in our database.
Step 5. Add the PHP script to send push notifications
You would have heard about Rudy Huyn, the famous Windows Phone developer. He wrote a PHP script to send push notifications to windows phone 8. Here’s the project on codeplex project link. Just visit this link and download the windowsphonepush.php file. Add this file to the same folder where you have placed other PHP files created above.
- You can send toast notifications, tile notifications or raw notifications using windowsphonepush.php.
- push_toast function sends a toast notification with title and subtitle
- push_tile function sends a tile update with count, title and background image.
- push_raw function sends a raw notification which is displayed in a message box
- push_delay values specify the delay with which the notification should be delivered. The introductory article of this series had a table of values for different delay times.
Step 6. Add a file sendmessage.php to send notifications to your app
Lastly add a PHP script that will select all URIs from the database and send notifications to it. This PHP file has windowsphonepush.php included to it.
- This script file fetches all the notification URIs from the database and sends a push notification to each one of them
- This script also has a custom toast message which will be sent to the client’s app. You could make a form for this purpose otherwise it would not be convenient to edit this php file again and again