作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
按照教程显示 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/
我是一名优秀的程序员,十分优秀!