 |
|
Hi i am developing one windows phone app, i need to send and receive toast push notification from my app.i am using php for send push notification and i want to know ,how to communicating with my cloud service and send uri to cloud service.i am using code given below, please any one help how to send push notification from my app using php
Code (C#)
/// Holds the push channel that is created or found.
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "ToastSampleChannel";
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the channel.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
pushChannel.ChannelUri.ToString()));
}
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
);
}
void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
StringBuilder message = new StringBuilder();
string relativeUri = string.Empty;
message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());
// Parse out the information that was part of the message.
foreach (string key in e.Collection.Keys)
{
message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);
if (string.Compare(
key,
"wp:Param",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.CompareOptions.IgnoreCase) == 0)
{
relativeUri = e.Collection[key];
}
}
// Display a dialog of all the fields in the toast.
Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString()));
}
Code (PHP)
<?php
/**
*Windows Phone 7 Push Notification in php by Rudy HUYN
**/
final class WindowsPhonePushDelay
{
const Immediate=0;
private function __construct(){}
}
class WindowsPhonePushNotification
{
private $notif_url = '';
function WindowsPhonePushNotification($notif_url)
{
$this->notif_url = $notif_url;
}
public function push_toast($message,$delay = WindowsPhonePushDelay::Immediate, $message_id)
{
$msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
"<wp:Notification xmlns:wp=\"WPNotification\">" .
"<wp:Toast>" .
"<wp:Text2>".htmlspecialchars($message)."</wp:Text2>" .
"</wp:Toast>" .
"</wp:Notification>";
return $this->push('toast',$delay+2,$message_id, $msg);
}
private function push($target,$delay,$message_id,$msg)
{
$sendedheaders= array(
'Content-Type: text/xml',
'Accept: application/*',
"X-NotificationClass: $delay"
);
if($message_id!=NULL)
$sendedheaders[]="X-MessageID: $message_id";
if($target!=NULL)
$sendedheaders[]="X-WindowsPhone-Target:$target";
$req = curl_init();
curl_setopt($req, CURLOPT_HEADER, true);
curl_setopt($req, CURLOPT_HTTPHEADER,$sendedheaders);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $msg);
curl_setopt($req, CURLOPT_URL, $this->notif_url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
// echo $req;
$response = curl_exec($req);
echo $response;
curl_close($req);
$result=array();
foreach(explode("\n",$response) as $line)
{
$tab=explode(":",$line,2);
if(count($tab)==2)
$result[$tab[0]]=trim($tab[1]);
}
// echo $result."ghghgh";
return $result;
}
}
?>
Code (PHP)
<?php
$con=mysqli_connect("localhost","root","root","buk");
if (isset($_GET["regId"]) || isset($_GET["message"])) {
$regId = $_GET["regId"];
$message_id = $_GET["id"];
$registatoin_ids=array($regId);
$notif_url = $_GET["notify"];
$message = $_GET["message"];
$customer_id = $_GET["customer_id"];
include_once './GCM.php';
$query = mysqli_query($con,"select count(offer) as count from pim_productdeal where customer_id='$customer_id' and offer!=''");
$count = mysqli_fetch_array($query);
echo $count['count'];
// exit;
$gcm = new GCM();
$registatoin_ids = array($regId);
include_once './windowspush.php';
$windows_push = new WindowsPhonePushNotification($notif_url);
$res = $windows_push->push_toast($message,$delay = WindowsPhonePushDelay::Immediate, $message_id);
$result = $gcm->send_notification($registatoin_ids,$customer_id, $message);
echo json_encode($result);
echo $result;
}
?>
This is am using mycode, i got error given below:
This is am using mycode i got error like given below:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
ActivityId: dbe7bdce-ecfe-4eb5-aaf6-80854d4b663d
X-Server: AM3MPNSM023
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 23 Aug 2013 13:38:06 GMT
Content-Length: 11
Please any one tell me how to solve in this issue and how to communicate with cloud service using php file
Tag : Mobile, Device (Mobile), C#, Windows Phone, Windows, Clound Service
|
|
 |
 |
 |
 |
Date :
2013-08-24 14:06:25 |
By :
Dashi |
View :
1813 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |