Я пытался добавить Pu sh уведомлений с таргетингом iOS 10 в Xamarin iOS App, но
Foreground
я впервые получаю уведомление о развертывании в UNUserNotificationCenterDelegate
, но во второй раз он не фиксирует это, если я не удаляю приложение и не переустанавливаю его.
Background
я всегда получаю уведомление, но я не могу перехватить его касание. InActive
работает нормально, я могу захватить его кран в FinishedLaunching
.
Код
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// Remote Notifications
SetupRemoteNotifications(app, options);
}
private void SetupRemoteNotifications(UIApplication app, NSDictionary options)
{
// Handling multiple OnAppear calls
if (!UIApplication.SharedApplication.IsRegisteredForRemoteNotifications)
{
// register for remote notifications based on system version
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// iOS 10 or later
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
if (granted)
{
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
}
});
// Watch for notifications while the app is active
UNUserNotificationCenter.Current.Delegate = this;
}
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);
}
}
// Check for a notification
if (options != null)
{
// check for a remote notification
if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
{
var remoteNotification = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
if (remoteNotification != null)
{
ProcessNotification(remoteNotification, true);
//new UIAlertView(remoteNotification.AlertAction, remoteNotification.AlertBody, null, "OK", null).Show();
}
}
}
}
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response,
Action completionHandler)
{
var userInfo = response.Notification.Request.Content.UserInfo;
if (userInfo != null && userInfo.ContainsKey(new NSString("aps")))
{
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
NSDictionary message = aps.ObjectForKey(new NSString("alert")) as NSDictionary;
NSDictionary metadataNSDict = aps.ObjectForKey(new NSString("metadata")) as NSDictionary;
var metaDataDict = metadataNSDict.ConvertToDictionary();
var metaJson = metaDataDict.FromDictionaryToJson();
var metadata = JsonConvert.DeserializeObject<NotificationMetadata>(metaJson);
if (message != null && metadata != null)
{
NotificationGoToPage(metadata);
}
}
completionHandler();
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification,
Action<UNNotificationPresentationOptions> 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 |
UNNotificationPresentationOptions.Sound |
UNNotificationPresentationOptions.Badge);
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
ProcessNotification(userInfo, false);
}
Даже я пытался использовать DidReceiveRemoteNotification
но это не захватывает Background
[Export("didReceiveRemoteNotification:")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
Action<UIBackgroundFetchResult> completionHandler)
{
if (application.ApplicationState == UIApplicationState.Active)
{
completionHandler(UIBackgroundFetchResult.NoData);
}
else if (application.ApplicationState == UIApplicationState.Background)
{
}
else
{
completionHandler(UIBackgroundFetchResult.NoData);
}
}
Entitlements.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>production</string>
</dict>
</plist>
Info.plist
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>