C# 用户登录后,如何在iOS中注册推送通知?
当用户第一次打开我的应用程序时,他们会收到大量权限请求,其中之一就是推送通知。我见过一些人在Swift中引用方法,但在Xamarin中没有 我的应用可以注册并接收推送通知,但我更希望在用户成功登录后请求推送通知。在Xamarin Forms的世界里这可能吗 如果不是所有的代码都在AppDelegate.cs中,我理解在Microsoft Docs()上使用该示例很多。我不明白为什么它会出现在这里,有没有人能提供一些指导或阅读材料来解释为什么会出现在这里 我们将非常感谢您的任何帮助 更新 所以我现在正在慢慢地到达那里,我正在使用一个接口从共享代码调用iOS项目中的C# 用户登录后,如何在iOS中注册推送通知?,c#,ios,xamarin,C#,Ios,Xamarin,当用户第一次打开我的应用程序时,他们会收到大量权限请求,其中之一就是推送通知。我见过一些人在Swift中引用方法,但在Xamarin中没有 我的应用可以注册并接收推送通知,但我更希望在用户成功登录后请求推送通知。在Xamarin Forms的世界里这可能吗 如果不是所有的代码都在AppDelegate.cs中,我理解在Microsoft Docs()上使用该示例很多。我不明白为什么它会出现在这里,有没有人能提供一些指导或阅读材料来解释为什么会出现在这里 我们将非常感谢您的任何帮助 更新 所以我现
UserNotificationCenterDelegate
类
AppDelegate.cs
public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
// Unsure if this line is required or not
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
base.RegisteredForRemoteNotifications(application, deviceToken);
Console.WriteLine("Hello");
}
用户通知
public UserNotificationCenterDelegate() { }
public void RegisterForPushNotifications() {
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge, (approved, error) => {
if (approved) {
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
}
});
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
base.WillPresentNotification(center, notification, completionHandler);
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification);
// Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert);
}
public UserNotificationCenterDelegate(){}
公共无效注册表orpushNotifications(){
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge,(已批准,错误)=>{
(如获批准){
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemotonifications);
}
});
}
public override void WillPresentNotification(未使用通知中心、未通知通知通知、操作完成处理程序){
base.WillPresentNotification(中心、通知、完成处理程序);
//对通知做些什么
WriteLine(“活动通知:{0}”,通知);
//告诉系统以任何方式显示通知或使用
//“没有”,也就是说我们已经在本地处理了这次展览。
completionHandler(UNNotificationPresentationOptions.Alert);
}
因此,通过DependencyService,我可以调用
RegisterForPushNotifiactions
,但在接受推送权限时,它应该调用AppDelegate.cs中的RegisterForRemotonitifications
,但它没有。我认为您不必从应用程序代理内部请求推送权限。从您阅读的文章中,删除以下方法中的所有代码,该方法要求具有推送通知权限:
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
PushHelperiOS.CheckAndAskPushNotificationPermission();
然后,将该代码移动到其他一些类/函数中,以便在稍后准备请求推送权限时调用这些类/函数。比如:
public static void CheckAndAskPushNotificationPermission()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{ UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(granted, error) =>
{
if (granted)
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);
}
}
无论何时何地,只要您准备请求许可:
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
PushHelperiOS.CheckAndAskPushNotificationPermission();
在应用程序委托中保留以下方法不变,因为您只能在其中重写它们(根据您的博客):
如果您有任何疑问,请告诉我。看起来您需要做的就是将给定示例中的
FinishedLaunching
的所有内容移动到您希望调用它的任何位置。其余部分必须仍然在AppDelegate
中,因为它由重写方法组成。但我的理解是,我的代码在应用程序运行时仍然会被执行吗?我只希望在用户成功登录时尝试注册推送通知/请求权限。否。无论何时何地调用该代码,都会执行该代码。目前,它是在应用程序启动时执行的,因为在应用程序完成启动时调用它。但是你可以随时打电话给它。一旦你调用它,你的应用程序将与操作系统通信请求。操作系统将通过你的应用程序代理通知你的应用程序,你的应用程序代理就是这样设计的。很抱歉,我误读了你的话。我知道你现在从哪里来。我已成功获得推送通知权限,可以立即显示在其他页面上(而不是应用程序启动时)。我只是想从另一个类中溢出注册表项以进行删除。这是不可能的。你只能转接电话。您的应用程序代理需要有权访问您要将呼叫转发到的类。如果没有更好的方法可以使用静态属性链接它们。因此,delegate将调用实例AppDelegate.notificationListener.register,因此在本例中,notificationListener是一个静态属性,您可能会在一些构造函数中为其赋值,如AppDelegate.notificationListener=thisGotcha,由于我使用的是Xamarin表单,我想通过界面访问什么CheckAndAskPushNotificationPermission
,对吗?对,在共享平台中创建接口,然后在平台中实现上述实现,然后作为依赖项服务注册和访问它们。这样我就可以通过接口调用myRegisterForPushNotification
方法。在此范围内,如果权限已获得批准,则应调用AppDelegate中覆盖的注册表项或消息。但是,它并没有调用它。在从VS成功重新部署之前,请确保从设备中删除现有应用。我已经完成了!我上面的例子是可行的,但我不知道问题出在哪里,直到我覆盖了远程通知的注册失败。我正在使用一个连接到APS的测试项目。因此,我使用APS将我的新代码移动到我的实际项目中,它完全按照我所希望的那样工作!