DidReceiveNotificationRequest 没有被调用
我有一个 xamarin 表单应用程序,当我从服务器收到通知时,没有调用 ios 通知服务扩展.
I have a xamarin forms application and ios Notification service extension is not getting called when I receive notification from server.
到目前为止,我做了以下几件事:
I have done the following things so far:
在 apns 负载中添加了
mutable-content = 1
.
这就是我在服务中操作 apns 负载的方式
This is how I manipulate the apns payload in the service
public class NotificationService : UNNotificationServiceExtension
{
Action<UNNotificationContent> ContentHandler { get; set; }
UNMutableNotificationContent BestAttemptContent { get; set; }
protected NotificationService(IntPtr handle) : base(handle)
{
}
public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
{
ContentHandler = contentHandler;
BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();
var newAlertContent = new UNMutableNotificationContent
{
Body = "Body from Service",
Title = "Title from Service",
Sound = BestAttemptContent.Sound,
Badge = 2
};
ContentHandler(newAlertContent);
}
public override void TimeWillExpire()
{
}
}
我还完成了通知服务扩展包 ID.(我的应用包 ID 是
com.companyname.appname.test
,扩展包 ID 是com.companyname.appname.test.xxxxServiceExtension
在 Finishlaunching 方法的 AppDelegate 类中,我还添加了权限代码.
In the AppDelegate class in Finishlaunching method I also have the permission code added.
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {
});
还有什么我需要做的吗?
Is there anything else that I need to do?
只有当远程通知的有效负载包含以下信息时,系统才会执行您的通知内容应用扩展:
The system executes your notification content app extension only when a remote notification’s payload contains the following information:
有效负载必须包含值为 1 的可变内容键.
The payload must include the mutable-content key with a value of 1.
负载必须包含带有标题、副标题或正文信息的警报字典.
The payload must include an alert dictionary with title, subtitle, or body information.
清单 2 显示了包含加密数据的通知负载的 JSON 数据.设置可变内容标志以便用户的设备知道运行相应的服务应用扩展,其代码显示在 .
Listing 2 shows the JSON data for a notification payload containing encrypted data. The mutable-content flag is set so that the user’s device knows to run the corresponding service app extension, the code for which is shown in .
清单 2指定远程通知负载
Listing 2 Specifying the remote notification payload
{
"aps" : {
"category" : "SECRET",
"mutable-content" : 1,
"alert" : {
"title" : "Secret Message!",
"body" : "(Encrypted)"
},
},
"ENCRYPTED_DATA" : "Salted__·öîQÊ$UDì_¶Ù∞è Ω^¬%gq∞NÿÒQùw"
}
您确定指定了警报键吗?
Are you sure that you specify alert key?