我正在编写一个针对 iOS 的 .Net8 MAUI 应用程序。应用程序应该能够接收远程 APNS 通知。当应用程序位于前台时,这可以正常工作。然后调用 WillPresentNotification 方法并处理通知。
我无法开始工作的是在后台处理通知,而无需用户单击通知。换句话说,我希望我的应用程序能够处理通知有效负载,即使用户没有单击通知。
我知道应该调用方法 DidReceiveRemoteNotification (...根据我发现的其他文章,像这样 one。不同之处在于我使用 .Net8 而不是 .Net7。我尝试修改那篇文章中提到的我的 csproj 文件对我不起作用。
我尝试触发的方法的 C# 代码如下所示:
[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
public void DidReceiveRemoteNotification(UIApplication application, NSDictionary notification, Action<UIBackgroundFetchResult> completionHandler)
{
//code to write the notification to the DB and display the local notification
completionHandler(UIBackgroundFetchResult.NewData);
}
[Foundation.Export("application:didReceiveRemoteNotification:")]
public void ReceivedRemoteNotification(UIKit.UIApplication application, Foundation.NSDictionary userInfo)
{
ProcessNotification(userInfo, false);
}
有趣的是,这个方法没有在 UNUserNotificationCenterDelegate 或 MauiUIApplicationDelegate 接口中定义,所以我实际上无法重写它。
有人设法使用 MAUI 和 .Net8 在 iOS 后台处理通知吗?
我也遇到同样的问题
您需要检查 Aps 定义,以确保在发送 Firebase 消息时设置了“ContentAvailable = true”属性。 这告诉您的应用程序该事件也在后台调用。
var aps = new Aps()
{
CustomData = iosNotificationDataObject,
Sound = "default",
ContentAvailable = true, //Tells the mobile app content is available
};
根据RemoteNotificationsProgramming
content-available
定义是
将此键的值设置为 1 表示有新内容 可用的。包含此键和值意味着当您的应用程序 在后台启动或恢复, 应用程序:didReceiveRemoteNotification:fetchCompletionHandler:是 打电话了。
(报刊亭应用程序保证能够收到至少一次推送 每 24 小时窗口使用此密钥。)
希望这有帮助:)