gpt4 book ai didi

c# - 无法从 Azure 移动服务将 toast 推送到 WP8

转载 作者:行者123 更新时间:2023-12-03 01:56:09 25 4
gpt4 key购买 nike

按照教程显示 Toast 通知时遇到一些问题

这里是 Azure 移动服务服务器脚本:

function insert(item, user, request) {
request.execute({
success: function () {
// Write to the response and then send the notification in the background
request.respond();
push.mpns.sendToast(item.channel, {
text1:"Sent from cloud!"
}, {
success: function (pushResponse) {
console.log("Sent push:", pushResponse);
}
});
}
});

这是我放入 App.xaml.cs 中的编码:

//push notification
public static HttpNotificationChannel CurrentChannel { get; private set; }


private void AcquirePushChannel()
{
CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");


if (CurrentChannel == null)
{
CurrentChannel = new HttpNotificationChannel("MyPushChannel");
CurrentChannel.Open();
//CurrentChannel.BindToShellTile();
CurrentChannel.BindToShellToast();
}
}

private void Application_Launching(object sender, LaunchingEventArgs e)
{
AcquirePushChannel();
}

但是 toast 仍然没有出来(翻转板运行良好)。

需要进行任何修改才能使 toast 正常工作吗?

编辑:打开 channel 时出错:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Open failed because the channel was already open. You can find an open channel by calling the Find method.
Source=Microsoft.Phone
StackTrace:
at Microsoft.Phone.Notification.SafeNativeMethods.ThrowExceptionFromHResult(Int32 hr, Exception defaultException, NotificationType type)
at Microsoft.Phone.Notification.HttpNotificationChannel.Open()
at UtemFtmkDB.App.AcquirePushChannel()
at UtemFtmkDB.App.Application_Launching(Object sender, LaunchingEventArgs e)
at Microsoft.Phone.Shell.PhoneApplicationService.FireLaunching()
at Microsoft.Phone.TaskModel.Interop.ITask.Launching.Invoke()
at Microsoft.Phone.TaskModel.Interop.Task.FireOnLaunching()
InnerException:

最佳答案

如果应用程序在前台运行时收到 toast 通知,则不会在 UI 中显示 toast;相反,您可以通过订阅 ShellToastNotificationReceived event 来接收它。如果这样做,您将在事件处理程序上收到通知。

问题更新后编辑:为了防止调用Open时出现InvalidOperationException,您可以使用以下代码:

private void AcquirePushChannel()
{
CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");

if (CurrentChannel == null)
{
CurrentChannel = new HttpNotificationChannel("MyPushChannel");
}

if (CurrentChannel.ConnectionStatus == ChannelConnectionStatus.Disconnected)
{
CurrentChannel.Open();
}

if (!CurrentChannel.IsShellToastBound)
{
CurrentChannel.BindToShellToast();
}
}

关于c# - 无法从 Azure 移动服务将 toast 推送到 WP8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com