Xamarin.Forms iOS DidReceiveNotificationResponse not called.

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

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();

    UNUserNotificationCenter.Current.Delegate = this;

    ...
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
{
    if (response.IsDefaultAction)
    {
        if (response.Notification.Request.Content.UserInfo.ContainsKey(new NSString("remainingTime")))
        {
            Locator.GetSharedInstance<IAlertDelayedService>().RaiseOpenAlertDelayedPopupEvent();
        }
    }
    completionHandler();
}

[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, System.Action<UNNotificationPresentationOptions> completionHandler)
{
    completionHandler(UNNotificationPresentationOptions.Alert);
}

}

英文:

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(&quot;AppDelegate&quot;)]
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();

        UNUserNotificationCenter.Current.Delegate = this;

        ...
    }

    [Export(&quot;userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:&quot;)]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
    {
        if (response.IsDefaultAction)
        {
            if (response.Notification.Request.Content.UserInfo.ContainsKey(new NSString(&quot;remainingTime&quot;)))
            {
                Locator.GetSharedInstance&lt;IAlertDelayedService&gt;().RaiseOpenAlertDelayedPopupEvent();
            }

        }
        completionHandler();
    }

    [Export(&quot;userNotificationCenter:willPresentNotification:withCompletionHandler:&quot;)]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, System.Action&lt;UNNotificationPresentationOptions&gt; completionHandler)
    {
        completionHandler(UNNotificationPresentationOptions.Alert);
    }

}

答案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:

确定