php推送到手机,PHP版的Windows Phone推送功能

本文介绍如何使用PHP实现Windows Phone的推送通知功能,包括Toast通知和Tile更新。通过获取设备的通知管道URI,利用MPNS服务发送XML数据来触发手机接收通知。代码示例展示了创建Toast和Tile通知的方法,并提供了延迟选项。
摘要由CSDN通过智能技术生成

经过几天的捣鼓,终于搞定了PHP版的Windows Phone推送功能(Toast通知)。

推送流程:

1.手机上部署XAP,然后获取到手机的通知管道URI。

2.通过通知管道URI向MPNS发送通知数据。

3.MPNS向手机推送通知。

IC505514.jpg

具体参考:http://msdn.microsoft.com/zh-cn/library/ff402558(v=vs.92)

推送成功效果图:

2000

PHP代码:/**

*Windows Phone 7 Push Notification in php by Rudy HUYN

**/

final class WindowsPhonePushDelay

{

const Immediate=0;

const In450Sec=10;

const In900Sec=20;

private function __construct(){}

}

class WindowsPhonePushNotification

{

private $notif_url = '';

function WindowsPhonePushNotification($notif_url)

{

$this->notif_url = $notif_url;

}

/**

* Toast notifications are system-wide notifications that do not disrupt the user workflow or require intervention to resolve. They are displayed at the top of the screen for ten seconds before disappearing. If the toast notification is tapped, the application that sent the toast notification will launch. A toast notification can be dismissed with a flick.

* Two text elements of a toast notification can be updated:

* Title. A bolded string that displays immediately after the application icon.

* Sub-title. A non-bolded string that displays immediately after the Title.

*/

public function push_toast($title, $subtitle,$delay = WindowsPhonePushDelay::Immediate, $message_id=NULL)

{

$msg = "" .

"" .

"" .

"".htmlspecialchars($title)."" .

"".htmlspecialchars($subtitle)."" .

"" .

"";

return $this->push('toast',$delay+2,$message_id, $msg);

}

/**

*A Tile displays in the Start screen if the end user has pinned it. Three elements of the Tile can be updated:

*@background_url : You can use a local resource or remote resource for the background image of a Tile.

*@title : The Title must fit a single line of text and should not be wider than the actual Tile. If this value is not set, the already-existing Title will display in the Tile.

*@count. an integer value from 1 to 99. If not set in the push notification or set to any other integer value, the current Count value will continue to display.

*/

public function push_tile($background_url, $title, $count, $delay = WindowsPhonePushDelay::Immediate,$message_id=NULL)

{

$msg = "" .

"" .

"" .

"".htmlspecialchars($background_url)."" .

"$count" .

"".htmlspecialchars($title)."" .

"" .

"";

return $this->push('token',$delay+1, $message_id,$msg);

}

/**

* If you do not wish to update the Tile or send a toast notification, you can instead send raw information to your application using a raw notification. If your application is not currently running, the raw notification is discarded on the Microsoft Push Notification Service and is not delivered to the device. The payload of a raw notification has a maximum size of 1 KB.

*/

public function push_raw($data, $delay = WindowsPhonePushDelay::Immediate,$message_id=NULL)

{

return $this->push(NULL,$delay+3,$message_id, $data);

}

/**

*@target : type of notification

*@delay : immediate, in 450sec or in 900sec

*@message_id : The optional custom header X-MessageID uniquely identifies a notification message. If it is present, the same value is returned in the notification response. It must be a string that contains a UUID

*/

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);

$response = curl_exec($req);

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]);

}

return $result;

}

}

代码来自:http://jpsolution.wordpress.com/tag/push-notification-for-windows-phone-7-in-php/

其他参考资料:http://www.daveamenta.com/2010-11/send-push-notifications-to-windows-phone-7-from-php/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值