首页 > 解决方案 > DidReceiveNotificationRequest 没有被调用

问题描述

我有一个 xamarin 表单应用程序,当我收到来自服务器的通知时,没有调用 ios 通知服务扩展。

到目前为止,我做了以下事情:

  1. mutable-content = 1在 apns 有效负载中添加了 。

  2. 这就是我在服务中操作 apns 有效负载的方式

    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()
        {
        }
    }
  1. 我还完成了通知服务扩展包 ID。(我的应用包 ID 是com.companyname.appname.test,扩展包 ID 是com.companyname.appname.test.xxxxServiceExtension

  2. 在 Finishlaunching 方法的 AppDelegate 类中,我还添加了权限代码。

  UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {
            });

还有什么我需要做的吗?

标签: iosxamarin.iosapple-push-notifications

解决方案


仅当远程通知的有效负载包含以下信息时,系统才会执行您的通知内容应用程序扩展:

有效负载必须包含值为 1 的 mutable-content 键。

有效负载必须包含带有标题、副标题或正文信息的警报字典。

清单 2 显示了包含加密数据的通知负载的 JSON 数据。设置了 mutable-content 标志,以便用户的设备知道运行相应的服务应用程序扩展,其代码显示在 .

清单 2 指定远程通知负载

{
   “aps” : {
      “category” : “SECRET”,
      “mutable-content” : 1,
      “alert” : {
         “title” : “Secret Message!”,
         “body”  : “(Encrypted)”
     },
   },
   “ENCRYPTED_DATA” : “Salted__·öîQÊ$UDì_¶Ù∞è   Ω^¬%gq∞NÿÒQùw”
}

你确定你指定了警报键吗?


推荐阅读