Xamarin.Forms iOS DidReceiveNotificationResponse not called.

huangapple go评论111阅读模式
英文:

Xamarin.Forms iOS DidReceiveNotificationResponse not called

问题

I am trying to use local notifications in my Xamarin.Forms iOS application. The notification is visible, but when I tap the notification, DidReceiveNotificationResponse is not called at all. The code that manages the authorization is not shown here but it is correct. I have read all the posts here and elsewhere about this problem that seams to be related to a delegate issue (not assigned). Here is my code:

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Popup.Init();
global::Xamarin.Forms.Forms.Init();

  1. UNUserNotificationCenter.Current.Delegate = this;
  2. ...
  3. }
  4. [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
  5. public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
  6. {
  7. if (response.IsDefaultAction)
  8. {
  9. if (response.Notification.Request.Content.UserInfo.ContainsKey(new NSString("remainingTime")))
  10. {
  11. Locator.GetSharedInstance<IAlertDelayedService>().RaiseOpenAlertDelayedPopupEvent();
  12. }
  13. }
  14. completionHandler();
  15. }
  16. [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
  17. public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, System.Action<UNNotificationPresentationOptions> completionHandler)
  18. {
  19. completionHandler(UNNotificationPresentationOptions.Alert);
  20. }

}

英文:

I am trying to use local notifications in my Xamarin.Forms iOS application.

The notification is visible, but when I tap the notification, DidReceiveNotificationResponse is not called at all. The code that manages the authorization is not shown here but it is correct.

I have read all the posts here and elsewhere about this problem that seams to be related to a delegate issue (not assigned).

Here is my code:

  1. [Register(&quot;AppDelegate&quot;)]
  2. public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate
  3. {
  4. public override bool FinishedLaunching(UIApplication app, NSDictionary options)
  5. {
  6. Popup.Init();
  7. global::Xamarin.Forms.Forms.Init();
  8. UNUserNotificationCenter.Current.Delegate = this;
  9. ...
  10. }
  11. [Export(&quot;userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:&quot;)]
  12. public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
  13. {
  14. if (response.IsDefaultAction)
  15. {
  16. if (response.Notification.Request.Content.UserInfo.ContainsKey(new NSString(&quot;remainingTime&quot;)))
  17. {
  18. Locator.GetSharedInstance&lt;IAlertDelayedService&gt;().RaiseOpenAlertDelayedPopupEvent();
  19. }
  20. }
  21. completionHandler();
  22. }
  23. [Export(&quot;userNotificationCenter:willPresentNotification:withCompletionHandler:&quot;)]
  24. public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, System.Action&lt;UNNotificationPresentationOptions&gt; completionHandler)
  25. {
  26. completionHandler(UNNotificationPresentationOptions.Alert);
  27. }
  28. }

答案1

得分: 0

I have found a solution in a Xamarin.Forms notification sample here.
It uses a UNUserNotificationCenterDelegate derived class to handle the received and tapped notifications.

英文:

I have found a solution in a Xamarin.Forms notification sample here.
It uses a UNUserNotificationCenterDelegate derived class to handle the received and tapped notifications.

huangapple
  • 本文由 发表于 2023年4月6日 21:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定