如何从推送通知中获取数据?

时间:2014-02-18 11:54:58

标签: windows-phone-8 notifications push-notification mpns

我正在通过节点js脚本使用azure通知中心发送推送通知。我可以发送和接收推送通知。我不知道如何检索数据。我按如下方式发送推送: -

function sendNotifications(pushMessage) {
    console.log('inside sendNotifications');
    var hub = azure.createNotificationHubService('hubname','connection string');

    hub.mpns.sendToast(
        null,
        {
            text1: pushMessage,
            text2: 'some data'
        },
        function (error)
        {
            if (!error)
            {
                //message send successfully
                console.log("mpns.sendToast push success: "+error);
                RESPONSE.send(statusCodes.OK, { ResponseMessage : 'mpns.sendToast message success' });
            }
            else
            {
                // msg failed to send
                console.log("errro error.shouldDeleteChannel: "+error);
                RESPONSE.send(statusCodes.OK, { ResponseMessage :'mpns.sendToast message error '+error });
            }
        });
}

我想在收到的申请表中收到text1和text2。你能告诉我怎么做吗?或者,如果我想推送一些数据,是否需要以不同方式发送推送通知?如何通过推氮化推送数据?我还能推出多大的数据?

1 个答案:

答案 0 :(得分:1)

如果您的应用在收到Toast通知时已经打开,则以下事件处理程序可以获取通知的参数(例如e.Collection[wp:Text1]将返回Toast的标题):

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

}

如果通过单击Toast通知打开了您的应用,则可以在打开应用的页面中实施以下方法。您可以访问在Toast通知的wp:Param参数的查询字符串中传递的参数。我不知道如何在这个方法中获得Text1和Text2。

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    //  If we navigated to this page
    // from the MainPage, the DefaultTitle parameter will be "FromMain".  If we navigated here
    // when the secondary Tile was tapped, the parameter will be "FromTile".
    textBlockFrom.Text = "Navigated here from " + this.NavigationContext.QueryString["NavigatedFrom"];

}

代码示例来自here