Issue with Xamarin iOS Rich Push Release

I am trying to implement media / rich / advanced notifications in ios. I have an iPhone 6, 6, and 7. The image I'm sending to the payload appears in the rich notification at 6 , but not at 6 or 7 . The code seems to just stop at CreateDownloadTask (I checked that I can change the body text just before this line of code, but can't after that). I even had a simpler version of this using NSData.FromUrl (url), but the code "breaks" on that line. It's weird to think that it really won't break, it displays the text for the Body element that was originally clicked. Even trying to catch won't catch the error. FYI..category exists for custom ui I create. Can't figure out why it only works on iphone 6 (all phones are at 10.2.x or higher)

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

I can't submit the 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

+3


source to share





All Articles