Xamarin iOS富推送通知问题

Xamarin iOS富推送通知问题,xamarin,remote-notifications,rich-notifications,Xamarin,Remote Notifications,Rich Notifications,我正在尝试在ios中实现媒体/丰富/增强通知。我有一部iphone6、6s和7。我在有效负载中发送的图像会出现在6上的rich通知中,但不会出现在6或7上。代码似乎只是在CreateDownloadTask处停止(我已经验证了我可以在代码行之前更改正文文本,但在代码行之后不能更改)。我甚至有一个更简单的版本使用NSData.FromUrl(url),但代码在那一行“断开”。奇怪的是,它并没有真正中断,它显示了最初被推送的Body元素的文本。即使尝试捕捉也不能抓住错误。仅供参考..类别用于我正在构

我正在尝试在ios中实现媒体/丰富/增强通知。我有一部iphone6、6s和7。我在有效负载中发送的图像会出现在6上的rich通知中,但不会出现在6或7上。代码似乎只是在CreateDownloadTask处停止(我已经验证了我可以在代码行之前更改正文文本,但在代码行之后不能更改)。我甚至有一个更简单的版本使用NSData.FromUrl(url),但代码在那一行“断开”。奇怪的是,它并没有真正中断,它显示了最初被推送的Body元素的文本。即使尝试捕捉也不能抓住错误。仅供参考..类别用于我正在构建的自定义ui无法理解为什么它只能在iphone 6上正常工作(所有手机都在10.2.x或更高版本上)

有效负载为{“aps”:{“alert”:{“title”:“title”,“subtitle”:“subtitle”,“body”:“body”},“category”:“pushWithImage”,“可变内容”:1},“pushImage”:“}”

我无法发送项目,但以下是服务扩展代码:

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Foundation;
using SDWebImage;
using UIKit;
using UserNotifications;

namespace notifications
{
[Register ("NotificationService")]
public class NotificationService : UNNotificationServiceExtension
{
UNMutableNotificationContent BestAttemptContent { get; set; }
public Action ContentHandler { get; set; }
const string ATTACHMENT_IMAGE_KEY = "pushImage";
const string ATTACHMENT_FILE_NAME = "-attachment-image.";
protected NotificationService (IntPtr handle) : base (handle)
{
// Note: this .ctor should not contain any initialization logic.
}

    public async Task<byte[]> LoadImage (string imageUrl)
    {
        var httpClient = new HttpClient ();
        var contentsTask = await httpClient.GetByteArrayAsync (imageUrl);

        return contentsTask;
    }

    public override void DidReceiveNotificationRequest (UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
    {
        string imageURL = null;

        ContentHandler = contentHandler;
        BestAttemptContent = request.Content.MutableCopy () as UNMutableNotificationContent;

        if (BestAttemptContent != null) {
            if (BestAttemptContent.UserInfo.ContainsKey (new NSString (ATTACHMENT_IMAGE_KEY))) {
                imageURL = BestAttemptContent.UserInfo.ValueForKey (new NSString (ATTACHMENT_IMAGE_KEY)).ToString ();
            }

            if (imageURL == null) {
                ContentHandler (BestAttemptContent);
                return;
            }

            var url = new NSUrl (imageURL.ToString ());

            NSError err = null;

            var task = NSUrlSession.SharedSession.CreateDownloadTask ( new NSMutableUrlRequest (url),(tempfile, response, error) => {

                if (error != null)
                {
                    ContentHandler (BestAttemptContent);
                    return;
                }
                if (tempfile == null)
                {
                    ContentHandler (BestAttemptContent);
                    return;
                }

                var cache = NSSearchPath.GetDirectories (NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User, true);
                var cachesFolder = cache [0];
                var guid = NSProcessInfo.ProcessInfo.GloballyUniqueString;
                var fileName = guid + ".png";
                var cacheFile = cachesFolder + "/" + fileName;
                var attachmentURL = NSUrl.CreateFileUrl (cacheFile, false, null);

                NSFileManager.DefaultManager.Move(tempfile, attachmentURL, out err);
                if (err != null)
                {
                    ContentHandler (BestAttemptContent);
                    return;
                }

                // Create attachment;
                var attachmentID = "image";
                var options = new UNNotificationAttachmentOptions ();
                var attachment = UNNotificationAttachment.FromIdentifier (attachmentID, attachmentURL, options, out err);

                BestAttemptContent.Attachments = new UNNotificationAttachment [] { attachment };
                BestAttemptContent.Title = BestAttemptContent.Title;
                BestAttemptContent.Body = BestAttemptContent.Body;
                BestAttemptContent.CategoryIdentifier = BestAttemptContent.CategoryIdentifier;
                BestAttemptContent.Subtitle = BestAttemptContent.Subtitle;
                //Display notification
                ContentHandler (BestAttemptContent);
            });
            task.Resume ();

        } else {
            // Display notification
            ContentHandler (BestAttemptContent);
        }
    }

    public override void TimeWillExpire ()
    {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        ContentHandler (BestAttemptContent);
    }
}
}
使用系统;
Net系统;
使用System.Net.Http;
使用System.Threading.Tasks;
使用基础;
使用SDWebImage;
使用UIKit;
使用用户通知;
命名空间通知
{
[注册(“通知服务”)]
公共类NotificationService:UnnotificationService扩展
{
UNMutableNotificationContent BestAttemptContent{get;set;}
公共操作ContentHandler{get;set;}
常量字符串附件\u IMAGE\u KEY=“pushImage”;
常量字符串ATTACHMENT\u FILE\u NAME=“-ATTACHMENT image.”;
受保护的通知服务(IntPtr句柄):基本(句柄)
{
//注意:此.ctor不应包含任何初始化逻辑。
}
公共异步任务LoadImage(字符串imageUrl)
{
var httpClient=newhttpclient();
var contentsTask=await httpClient.GetByteArrayAsync(imageUrl);
返回contentsTask;
}
公共覆盖void DidReceiveNotificationRequest(UNNotificationRequest请求,Action contentHandler)
{
字符串imageURL=null;
ContentHandler=ContentHandler;
BestAttemptContent=request.Content.MutableCopy()作为UNMutableNotificationContent;
如果(BestAttemptContent!=null){
if(BestAttemptContent.UserInfo.ContainsKey(新NSString(附件\图像\密钥))){
imageURL=BestAttemptContent.UserInfo.ValueForKey(新的NSString(附件图片键)).ToString();
}
if(imageURL==null){
ContentHandler(BestAttemptContent);
返回;
}
var url=new NSUrl(imageURL.ToString());
NSError err=null;
var task=NSUrlSession.SharedSession.CreateDownloadTask(新的NSMutableUrlRequest(url),(临时文件、响应、错误)=>{
if(错误!=null)
{
ContentHandler(BestAttemptContent);
返回;
}
if(tempfile==null)
{
ContentHandler(BestAttemptContent);
返回;
}
var cache=NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory,NSSearchPathDomain.User,true);
var cachesFolder=cache[0];
var guid=NSProcessInfo.ProcessInfo.GloballyUniqueString;
var fileName=guid+“.png”;
var cacheFile=cachesFolder+“/”+文件名;
var attachmentURL=NSUrl.CreateFileUrl(cacheFile,false,null);
NSFileManager.DefaultManager.Move(tempfile、attachmentURL、out err);
if(err!=null)
{
ContentHandler(BestAttemptContent);
返回;
}
//创建附件;
var attachmentID=“image”;
var options=newunnotificationattachmentoptions();
var attachment=UNNotificationAttachment.FromIdentifier(attachmentID、attachmentURL、options、out err);
BestAttemptContent.Attachments=newunnotificationAttachment[]{attachment};
BestAttemptContent.Title=BestAttemptContent.Title;
BestAttemptContent.Body=BestAttemptContent.Body;
BestAttemptContent.CategoryIdentifier=BestAttemptContent.CategoryIdentifier;
BestAttemptContent.Subtitle=BestAttemptContent.Subtitle;
//显示通知
ContentHandler(BestAttemptContent);
});
task.Resume();
}否则{
//显示通知
ContentHandler(BestAttemptContent);
}
}
公共覆盖无效时间将过期()
{
//在系统将终止扩展之前调用。
//将此作为一个机会,交付您对修改内容的“最佳尝试”,否则将使用原始推送负载。
ContentHandler(BestAttemptContent);
}
}
}

Bob,你能解决这个问题吗?