我使用插件。Firebase和我自己的APN服务器。我可以发送一条带有警报有效载荷的信息。这工作得很好。如果我用内容可用的有效负载发送消息,什么也不会发生。
我使用的是。net 7的当前版本。我的目标是iOS 16。我的测试设备是运行iOS 15.7的iPhone 12。
下面是我的AppDelegate的一些关键组件。
已添加到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;
});
}
});
这是DidReceiveRemoteNotification。
https://learn.microsoft.com/en-us/dotnet/api/uikit.uiapplicationdelegate.didreceiveremotenotification?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);
}
这是ReceivedRemoteNotification
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);
}
这是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";
}
这是ContentAvailable的样子
[JsonProperty("content-available")]
public int ContentAvailable { get; set; }
最后,我为静默推送更改了以下标头:
apns-push-type = "background"
apns-priority = 5
我注意到DidReceiveRemoteNotification和ReceivedRemoteNotification的覆盖没有找到。这应该被添加到。net 7 Maui版本中。我还注意到,除了DidReceiveRemoteNotification中的fetchCompletionHandler之外,这两种方法的装饰导出是相同的。
下面是一些关于如何工作的附加信息。
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&标签= macos
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app
有人知道为什么这不起作用吗?谢谢!
静音推送通知将在应用程序打开并处于前台时调用此方法。
// 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
BEGIN UPDATE 2
在添加答案中发现的修复后,所有静默推送通知现在都由DidReceiveRemoteNotification处理。
END UPDATE 2
确保在您的csprog文件中有这个。我现在能够在应用程序打开/正在使用时,在后台打开/运行时以及根本没有运行时接收静音推送通知。
https://github.com/dotnet/maui/issues/6259 issuecomment - 1109359755
我的是这样的。
<PropertyGroup>
<MtouchExtraArgs>--optimize:-static-block-to-delegate-lookup</MtouchExtraArgs>
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>
奇怪的是,所有的静默推送消息都是由DidReceiveRemoteNotification处理的。