问题描述
我已经创建了一个示例应用程序,用于在iOS上测试推送通知。我在PC上运行Visual Studio 2019,该PC通过本地网络连接到MAC。 iPhone通过避雷线连接到MAC。顾名思义,当我测试从Azure发送时,永远不会调用ReceivedRemoteNotification方法。 Azure通知我测试消息已成功发送。
我已经多次严格遵循本指南:
RegisteredForRemoteNotifications被调用,因此该应用似乎成功注册了推送通知。我还会在我接受的物理设备上获得许可请求。但是我注意到,当我将鼠标悬停在此方法内的“ Hub”上时,在类句柄下会出现错误:
“无法将类型为“ Mono.Debugger.soft.PointerValue”的对象强制转换为类型为“ Mono.Debugger.soft.PrimitiveValue”。”
但是我不确定这是否真的与我的问题有关。除了此链接之外,我找不到任何在线内容,这仅表明针对该错误的一种解决方法是在发行版中运行而不是调试。
namespace PushNotifTestApp.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application,as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
private SBNotificationHub Hub { get; set; }
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window,load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method,or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app,NSDictionary options)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationoptions.Alert | UNAuthorizationoptions.Badge | UNAuthorizationoptions.sound,(granted,error) => InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications));
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.sound,new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app,options);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application,NSError error)
{
}
public override void RegisteredForRemoteNotifications(UIApplication application,NSData devicetoken)
{
Hub = new SBNotificationHub(Constants.ListenConnectionString,Constants.NotificationHubName);
Hub.Unregisterall(devicetoken,(error) => {
if (error != null)
{
System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}",error.ToString());
return;
}
NSSet tags = null; // create tags if you want
Hub.RegisterNative(devicetoken,tags,(errorCallback) => {
if (errorCallback != null)
System.Diagnostics.Debug.WriteLine("RegisterNative error: " + errorCallback.ToString());
});
});
}
public override void ReceivedRemoteNotification(UIApplication application,NSDictionary userInfo)
{
ProcessNotification(userInfo,false);
}
void ProcessNotification(NSDictionary options,bool fromFinishedLaunching)
{
// Check to see if the dictionary has the aps key. This is the notification payload you would have sent
if (null != options && options.ContainsKey(new Nsstring("aps")))
{
//Get the aps dictionary
NSDictionary aps = options.ObjectForKey(new Nsstring("aps")) as NSDictionary;
string alert = string.Empty;
//Extract the alert text
// NOTE: If you're using the simple alert by just specifying
// " aps:{alert:"alert msg here"} ",this will work fine.
// But if you're using a complex alert with Localization keys,etc.,// your "alert" object from the aps dictionary will be another NSDictionary.
// Basically the JSON gets dumped right into a NSDictionary,// so keep that in mind.
if (aps.ContainsKey(new Nsstring("alert")))
alert = (aps[new Nsstring("alert")] as Nsstring).ToString();
//If this came from the ReceivedRemoteNotification while the app was running,// we of course need to manually process things like the sound,badge,and alert.
if (!fromFinishedLaunching)
{
//Manually show an alert
if (!string.IsNullOrEmpty(alert))
{
var myAlert = UIAlertController.Create("Notification",alert,UIAlertControllerStyle.Alert);
myAlert.AddAction(UIAlertAction.Create("OK",UIAlertActionStyle.Default,null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(myAlert,true,null);
}
}
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)