答案 0 :(得分:1)
在iOS上,您需要使用UNUserNotificationCenter通过修改AppDelegate类来请求授权:
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Ask for the permission to send notifications
UNUserNotificationCenter.Current.RequestAuthorization (UNAuthorizationOptions.Alert, (approved, err) => {
// User approved
});
return true;
}
在Android上,您无需分别请求推送通知权限,并且只要设置了INTERNET权限,就无需对代码进行任何更改。
答案 1 :(得分:1)
@Timo的答案是正确的,但很少有细微的东西值得注意。
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
//Register your app for remote notifications.
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
//iOS 10 or later
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
Console.WriteLine(granted);
});
//For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
//Messaging.SharedInstance.Delegate = this; //FCM
}
else
{
//iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
//App.Configure(); If using firebase
return true;
}