用PHP平铺通知

时间:2011-06-06 17:45:44

标签: php windows-phone-7 push-notification mpns

我正在尝试使用此库向手机发送磁贴通知 http://phpwindowsphonepush.codeplex.com/ 但是只有当我输入远程图像的URL时,手机才会收到通知。这是我简单的PHP代码

$uri="http://db3.notify.live.net/throttledthirdparty/01.00/AAEyQ7xxxxxxxxxxx";

$notif=new WindowsPhonePushNotification($uri);

$imm="http://www.mysite.net/imm/1.jpg";

$notif->push_tile($imm, "Title","2");

这是c#代码

private HttpNotificationChannel channel;

    public MainPage()
    {
        InitializeComponent();

        try
        {
            StartChannel();
        }
        catch (InvalidOperationException ioe)
        {
            HandleChannelException(ioe);
        }
    }



    private void HandleChannelException(InvalidOperationException ioe)
    {
        MessageBox.Show(ioe.Message);
    }

    private void StartChannel()
    {
        channel = HttpNotificationChannel.Find("test1");
        if (channel == null)
        {
            channel = new HttpNotificationChannel("test1", "name");
            AddDelegates();
            channel.Open();
        }
        else
        {
            AddDelegates();
        }

        if (channel.ChannelUri != null)
        {
            OnChannelReady();
        }
    }

    private void AddDelegates()
    {
        channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated);
        channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
        channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(channel_HttpNotificationReceived);
    }

    private void channel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
    {

        StreamReader sr = new StreamReader(e.Notification.Body, Encoding.UTF8);
        string text = sr.ReadToEnd();
        this.Dispatcher.BeginInvoke(() => MessageBox.Show(text));
    }



    private void channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {

        MessageBox.Show("ERROR");

    }

    private void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
        try
        {
            this.Dispatcher.BeginInvoke(OnChannelReady);
        }
        catch (InvalidOperationException ioe)
        {
            this.Dispatcher.BeginInvoke(() => HandleChannelException(ioe));
        }
    }

    private void OnChannelReady()
    {

        if (!this.channel.IsShellTileBound)
            this.channel.BindToShellTile();

    }

这是一个日志文件:

* About to connect() to db3.notify.live.net port 80
*   Trying xx.xx.xx.xxx... * connected
* Connected to db3.notify.live.net (xxx.xx.xx.xxx) port 80
> POST /throttledthirdparty/01.00/AAE0ykxoIaknSoMXxxxxxxxxxxxx HTTP/1.1
Host: db3.notify.live.net
Accept: */*
X-WindowsPhone-Target: token
X-NotificationClass: 1
Content-Length: 240
Content-Type: application/x-www-form-urlencoded
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
<wp:Tile>
<wp:BackgroundImage>http://www.mysite.net/imm/1.jpg</wp:BackgroundImage>
<wp:Count>2</wp:Count>
<wp:Title>6</wp:Title>
</wp:Tile> 
</wp:Notification>< HTTP/1.1 200 OK
< Cache-Control: private
< Server: Microsoft-IIS/7.5
< X-DeviceConnectionStatus: Connected
< X-NotificationStatus: Received
< X-SubscriptionStatus: Active
< X-MessageID: 00000000-0000-0000-0000-000000000000
< ActivityId: xxxxxx-83fa-423d-8240-ec610eca749b
< X-Server: DB3MPNSM005
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Tue, 07 Jun 2011 11:57:18 GMT
< Content-Length: 0
* Connection #0 to host db3.notify.live.net left intact
* Closing connection #0

1 个答案:

答案 0 :(得分:2)

好的,这将教会我仔细查看代码 - 如果不带参数调用BindToShellTile(),则只能使用本地资源作为磁贴。但是,如果您创建一个新集合并将该集合作为参数传递,您将能够访问该图块中的本地或远程图像。

例如

Collection<Uri> TileLocations = new Collection<Uri> { new Uri(@"/Background.jpg") };
TileLocations.Add(new Uri("http://jquery.andreaseberhard.de"));
if (!channel.IsShellTileBound) { channel.BindToShellTile(TileLocations); }

应该做的伎俩。 URI的集合应该包含可以从中检索图像的所有可能的域。在这种情况下,http://jquery.andreasebernhard.de处的任何内容都是有效图像。有关详细信息,请参阅this page

现在调用$notif->push_>tile("http://jquery.andreaseberhard.de/pngFix/pngtest.png","title","2");应该可以使用远程图像。