3

I am trying to implement media/rich/enhanced notifications in ios. I have an iPhone 6, 6s and 7. The image I send in the payload appears in the rich notification on the 6 , but not on the 6s or 7. The code seems to just stop at the CreateDownloadTask (I have verified that I can change the Body text just before that line of code, but I can’t after). I have even had simpler version of this use NSData.FromUrl(url) but the code “breaks” at that line. The odd think is that it doesn’t truly break, it displays the text for the Body element that was originally pushed. Even a try catch doesn’t grab the error. FYI..category is there for the custom ui I am building. Can't figure out why it only works properly on iphone 6 (all the phone are on 10.2.x or above)

the payload is {"aps":{"alert":{"title":"title", "subtitle":"subtitle","body":"body"}, "category":"pushWithImage","mutable-content":1}, "pushImage":"https://ewcweb.azurewebsites.net/media/boldlythumb.png"}

I can’t send project but below is the service extension code:

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);
    }
}
}

enter image description here

0 Answers0