我在xamarin表单UWP应用程序中添加了推送通知处理代码。
代码:
async Task InitRemoteNotificationAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
if (channel != null)
{
channel.PushNotificationReceived += OnPushNotificationReceived;
Debug.WriteLine($"Received token:{channel.Uri}");
}
}
private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
if (args.ToastNotification.Content.InnerText != null)
{
var msg = args.ToastNotification.Content.InnerText;
Xamarin.Forms.MessagingCenter.Send<object, string>(this, MyProject.App.NotificationReceivedKey, msg);
}
}
我在代码中添加了空检查。当我尝试从http://pushtestserver.azurewebsites.net/wns/推送测试通知时,收到以下异常。
在发送更新时捕获到异常:System.NullReferenceException:对象引用未设置为对象的实例。 在WebRole1.WNS.WebForm1.PostToWns(字符串机密,字符串sid,字符串uri,字符串xml,字符串通知类型,字符串contentType) 在WebRole1.WNS.WebForm1.btnDiyPush_Click(Object sender,EventArgs e)
屏幕截图: