Introduction
Here, we will see how to receive notifications in Windows 8.1 devices. For receiving Push notifications, we need a client and a server. Client is one which will receive notification and the server is one which will send notification to the client. We will create client in Windows phone 8.1 and server in PHP.
Client (WP 8.1 App)
Take a new project, i.e., Blank WP 8.1 App and give it whatever name you want.
You need to associate your app with Windows store first. So register your app in your developer account here. After registering your app at store, right click on your project in solution explorer and click on Store-->Associate App with Store...
You will get the below screen:
After login, you will get mobile number verification screen. After mobile number verification, you will get a list of all the apps that you have registered in your developer account. Select the app name and click Next button.
Create channel and generate uri. This uri is unique for any device. WNS will send notification to this uri. We can call it Device Token or Device Id.
string notificationUrl;
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
notificationUrl= channel.Uri.ToString();
It will be generated by client app itself and we need to give this uri to the server because the server will send notification using this uri.
Server (PHP)
For sending push notification to Windows phone 8.1 devices, we need the following three things:
- Package SID
- Client secret
- Uri
You will get Package SID and Client secret from your Windows developer account. In your developer account, go to Services-->Push notifications in left navigation bar. Then, click on Live Services site.
You will get Package SID and Client secret here.
Uri will be generated by client app and we have already seen this.
Create a php page and write the below code in it.
<html>
<head>
<title>Send Push Notification</title>
</head>
<body>
<?php
function sendPushNotification() {
$token="";
$message="Test message";
$XmlToastTemplate = '<?xml version="1.0"
encoding="UTF-8"?><toast launch="">
<visual lang="en-US">
<binding template="ToastText01">
<text id="1">'.$message.'</text>
</binding>
</visual>
</toast>';
if ($token == null || $token=="")
{
$token=createToken();
}
$sUrlWithToken="Uri with token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrlWithToken);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$XmlToastTemplate);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-WNS-Type: wns/toast',
'X-WNS-RequestForStatus: true',
'Content-Type: text/xml',
'Authorization: Bearer '.$token
));
$json = curl_exec($ch);
curl_close ($ch);
echo "Notification Sent";
}
function createToken()
{
$encSid = urlencode('Package Id');
$encSecret = urlencode('Client Secret');
$sUrl ="https://login.live.com/accesstoken.srf";
$body =
"grant_type=client_credentials&client_id=".$encSid.
"&client_secret=".$encSecret."&scope=notify.windows.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close ($ch);
$obj = json_decode($json);
return $obj->{'access_token'};
}
sendPushNotification();
?>
</body>
</html>
When you will run this page, on page load, you will receive notification in your app.