简体   繁体   English

带有静默推送通知 ReceivedRemoteNotification 或 DidReceiveRemoteNotification 的 .Net Maui iOS 应用程序永远不会被调用

[英].Net Maui iOS app with Silent Push Notifications ReceivedRemoteNotification or DidReceiveRemoteNotification never gets called

I'm using Plugin.Firebase and my own APN Server.我正在使用 Plugin.Firebase 和我自己的 APN 服务器。 I can send a message with an alert payload.我可以发送带有警报负载的消息。 This works just fine.这很好用。 If I send a message with a content-available payload, nothing happens.如果我发送带有内容可用负载的消息,则什么也不会发生。

I'm using the current version of the.Net 7 release.我使用的是.Net 7 的当前版本。 My target is iOS 16. My test device is an iPhone 12 running iOS 15.7.我的目标是 iOS 16。我的测试设备是运行 iOS 15.7 的 iPhone 12。

Here are some key components of my AppDelegate.以下是我的 AppDelegate 的一些关键组件。

This has been added to FinishedLaunching.这已添加到 FinishedLaunching。

var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
        UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
        {
            //this.Log($"RequestAuthorization: {granted}" + (error != null ? $" with error: {error.LocalizedDescription}" : string.Empty));

            if (granted && error == null)
            {
                this.InvokeOnMainThread(() =>
                {
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                    //this.InitFirebase();

                    UNUserNotificationCenter.Current.Delegate = this;
                });
            }
        }); 

Here is DidReceiveRemoteNotification.这是 DidReceiveRemoteNotification。

https://learn.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.didreceiveremotenotification?view=xamarin-ios-sdk-12 https://learn.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.didreceiveremotification?view=xamarin-ios-sdk-12

//App is in the foreground
    [Foundation.Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
    public void DidReceiveRemoteNotification(UIKit.UIApplication application, Foundation.NSDictionary userInfo, Action<UIKit.UIBackgroundFetchResult> completionHandler)
    {
        SentrySdk.CaptureMessage("DidReceiveNotificationResponse = " + "yes");
        
        ProcessNotification(userInfo, false);

        completionHandler(UIBackgroundFetchResult.NewData);

    }

Here is ReceivedRemoteNotification.这是 ReceivedRemoteNotification。

https://learn.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.receivedremotenotification?view=xamarin-ios-sdk-12 https://learn.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.receivedremotenotification?view=xamarin-ios-sdk-12

//App is in the background
    [Foundation.Export("application:didReceiveRemoteNotification:")]
    public void ReceivedRemoteNotification(UIKit.UIApplication application, Foundation.NSDictionary userInfo)
    {
        SentrySdk.CaptureMessage("ReceivedRemoteNotification = " + "yes");

        ProcessNotification(userInfo, false);

    }

Here is the APS payload.这是 APS 有效载荷。

public AppleNotificationSilent() 
        {
            //Id = id;

            Aps = new ApsPayload
            {
                //AlertBody = new ApsPayload.Alert
                //{
                //    Title = title,
                //    SubTitle = subtitle,
                //    Body = message
                //},

                //Alert = "",
                ContentAvailable = 1,
                //PushType = "background",
                //Priority = 5,
                CustomKey = "GetData" //THIS WORKS
            };
            //CustomKey = "GetData";
        }

Here is what ContentAvailable looks like.这是 ContentAvailable 的样子。

[JsonProperty("content-available")]
            public int ContentAvailable { get; set; }

Finally, I've changed the following headers for silent push.最后,我更改了以下标头以进行静默推送。

apns-push-type = "background"
apns-priority = 5

I've noticed that the overrides for DidReceiveRemoteNotification and ReceivedRemoteNotification aren't found.我注意到未找到 DidReceiveRemoteNotification 和 ReceivedRemoteNotification 的覆盖。 This was supposed to added to the.Net 7 Maui release.这应该添加到 .Net 7 Maui 版本中。 I've also noticed that the decoration Export for both methods is the same except for fetchCompletionHandler in DidReceiveRemoteNotification.我还注意到,除了 DidReceiveRemoteNotification 中的 fetchCompletionHandler 之外,这两种方法的装饰 Export 是相同的。

Here is some additional information on how this is supposed to work.这里有一些关于它应该如何工作的附加信息。

https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/updating-an-application-in-the-background https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/updating-an-application-in-the-background

https://learn.microsoft.com/en-us/xamarin/ios/platform/user-notifications/enhanced-user-notifications?source=recommendations&tabs=macos https://learn.microsoft.com/en-us/xamarin/ios/platform/user-notifications/enhanced-user-notifications?source=recommendations&tabs=macos

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

Does anyone know why this isn't working?有谁知道为什么这不起作用? Thanks!谢谢!

BEGIN UPDATE 1开始更新 1

A silent push notification will call this method when the app is open and in the foreground.当应用程序打开并位于前台时,静默推送通知将调用此方法。

// App is in the foreground
    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        SentrySdk.CaptureMessage("DidReceiveNotificationResponse = " + "yes");

        ShowLocalNotification();

        //this.Log($"{nameof(DidReceiveNotificationResponse)}: " + response.Notification.Request.Content.UserInfo);
        completionHandler();
    }

END UPDATE 1结束更新 1

BEGIN UPDATE 2开始更新 2

After adding the fix found in the answer, all silent push notifications are now handled by DidReceiveRemoteNotification.添加答案中找到的修复程序后,所有静默推送通知现在都由 DidReceiveRemoteNotification 处理。

END UPDATE 2结束更新 2

Make sure you have this in your csprog file.确保你的 csprog 文件中有这个。 I'm now able to receive silent push notifications when the app is open / being used, open / in the background, and while not running at all.我现在能够在应用程序打开/正在使用、打开/在后台以及根本未运行时接收静默推送通知。

https://github.com/dotnet/maui/issues/6259#issuecomment-1109359755 https://github.com/dotnet/maui/issues/6259#issuecomment-1109359755

Here is what mine looks like.这是我的样子。

<PropertyGroup>
    <MtouchExtraArgs>--optimize:-static-block-to-delegate-lookup</MtouchExtraArgs>
    <CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>

The weird part about this is that all the silent push messages are being handled by DidReceiveRemoteNotification.奇怪的是,所有静默推送消息都由 DidReceiveRemoteNotification 处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 永远不会调用ReceivedRemoteNotification和DidReceiveRemoteNotification - ReceivedRemoteNotification and DidReceiveRemoteNotification never get called DidReceiveRemoteNotification 不在后台调用 - Xamarin iOS - 推送通知 - DidReceiveRemoteNotification is not called in background - Xamarin iOS - Push Notifications 从未在Xamarin.iOS中调用DidReceiveRemoteNotification - DidReceiveRemoteNotification never called in Xamarin.iOS ReceivedRemoteNotification 和 DidReceiveRemoteNotification 的关系 - Relationship between ReceivedRemoteNotification and DidReceiveRemoteNotification APNS:如果应用未运行,会收到非静音通知吗? - APNS: Receive non silent notifications if app is not running? ThreadPool.QueueUserWorkItem 委托永远不会被调用 - ThreadPool.QueueUserWorkItem delegate never gets called Xamarin iOS本地推送通知 - Xamarin iOS Local Push Notifications 在 iOS 模拟器上调试推送通知 - Debugging push notifications on iOS emulator iOS 停止启动我的 Xamarin.iOS 应用程序来处理传入的推送通知 - iOS stops launching my Xamarin.iOS app to process incoming push notifications 用户点击应用程序图标时,iOS Xamarin Forms应用程序中不会触发Azure移动推送通知 - Azure mobile push notifications are not triggering in iOS Xamarin Forms app when user taps app icon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM