富推送通知不适用于

富推送通知不适用于

本文介绍了富推送通知不适用于 IOS 中的 FCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的项目中添加了 UNNotificationServiceExtensionUNNotificationContentExtension 以获取丰富的推送通知.
请参考下面我添加的代码.

I've added UNNotificationServiceExtension and UNNotificationContentExtension in my project for rich push notification.
Please refer the code below which i've added for the same.


代码:

#import "NotificationService.h"

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService {
    NSURLSession *session;
}

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSDictionary *userInfo = request.content.userInfo;
    if (userInfo == nil) {
        [self contentComplete];
        return;
    }

    if ([userInfo objectForKey:@"pic_url"]) {
        [self loadAttachmentForUrlString:[userInfo objectForKey:@"pic_url"]
                       completionHandler: ^(UNNotificationAttachment *attachment) {
                           self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
                       }];
    }
}

- (void)loadAttachmentForUrlString:(NSString *)urlString
                 completionHandler:(void (^)(UNNotificationAttachment *))completionHandler
{
    __block UNNotificationAttachment *attachment = nil;
    __block NSURL *attachmentURL = [NSURL URLWithString:urlString];

    NSString *fileExt = [@"." stringByAppendingString:[urlString pathExtension]];


    session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:attachmentURL
                                                completionHandler: ^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
                                                    if (error != nil)
                                                    {
                                                        NSLog(@"%@", error.localizedDescription);
                                                    }
                                                    else
                                                    {
                                                        NSFileManager *fileManager = [NSFileManager defaultManager];
                                                        NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path
                                                                                                  stringByAppendingString:fileExt]];
                                                        [fileManager moveItemAtURL:temporaryFileLocation
                                                                             toURL:localURL
                                                                             error:&error];

                                                        NSError *attachmentError = nil;
                                                        attachment = [UNNotificationAttachment attachmentWithIdentifier:[attachmentURL lastPathComponent]
                                                                                                                    URL:localURL
                                                                                                                options:nil
                                                                                                                  error:&attachmentError];
                                                        if (attachmentError)
                                                        {
                                                            NSLog(@"%@", attachmentError.localizedDescription);
                                                        }
                                                    }
                                                    completionHandler(attachment);
                                                }];

    [task resume];
}

- (void)serviceExtensionTimeWillExpire {
    // 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.
    [self contentComplete];
}

- (void)contentComplete
{
    [session invalidateAndCancel];
    self.contentHandler(self.bestAttemptContent);
}

@end

我正在使用以下有效负载

I'm using the following payload

    {
   "to": "9yJUWBA",
   "mutable_content": true,
   "category": "myNotificationCategory",
   "notification":
   {
      "title":"Realtime Custom Push Notifications",
       "subtitle":"Now with iOS 10 support!",
       "body":"Add multimedia content to your notifications"
   }
}

问题是我没有收到通知.我已经使用以下教程来实现丰富的推送通知.我检查了不同的可用答案,但没有一个对我有用.我还尝试通过附加扩展进程但未触发断点来调试 didReceiveNotificationRequest 方法.

The problem is i'm not getting the notification. I've used the following tutorial for implementing the rich push notification. I've checked different answers available but none of them worked for me. I've also tried to debug the didReceiveNotificationRequest method by attaching the extension process but the breakpoint not triggered.

https://mobisoftinfotech.com/resources/mguide/ios-10-rich-notifications-tutorial/

推荐答案

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler
{
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...
    self.bestAttemptContent.title = @"";
    self.bestAttemptContent.subtitle = @"";
//    self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", self.bestAttemptContent.body];

    NSDictionary *dictPushNotiData = request.content.userInfo;

    NSString *imageURL = @"";
    NSString *videoURL = @"";

    if(dictPushNotiData[@"xxx_details"])
    {
        NSString *jsonString = [dictPushNotiData objectForKey:@"xxx_details"];
        NSData *jsonData =  [jsonString dataUsingEncoding:NSUTF8StringEncoding];

        // If Instagram Notification Informations then call InstagramViewController
        if(jsonData)
        {
            NSMutableDictionary *dictXXX = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"dictXXX : %@",dictXXX);

            //self.bestAttemptContent.title = [NSString stringWithFormat:@"%@", [dictXXX objectForKey:@"xxxx"]];
            //self.bestAttemptContent.subtitle = [NSString stringWithFormat:@"%@", [dictXXX objectForKey:@"xxxx"]];

            NSString  *strBody = @"Notification xxxx x xxxx Post";

            if([[dictXXX objectForKey:@"xxxx"] length] > 0)
            {
                strBody = [dictXXX objectForKey:@"xxxx"];
            }

            self.bestAttemptContent.body = [NSString stringWithFormat:@"%@",strBody];

            imageURL = [dictXXX objectForKey:@"xxxx"];
            videoURL = [dictXXX objectForKey:@"xxxx"];
        }
    }

    NSString *strAttachment = nil;
    //    if (videoURL.length > 0)
    //    { //Prioritize videos over image
    //        strAttachment = videoURL;
    //    }
    //    else

    if (imageURL.length > 0)
    {
        strAttachment = imageURL;
    }
    else
    {
        self.contentHandler(self.bestAttemptContent); //Nothing to add to the push, return early.
        return;
    }

    // If there is an image in the payload, this part
    // will handle the downloading and displaying of the image.
    if (strAttachment) {
        NSURL *URL = [NSURL URLWithString:strAttachment];
        NSURLSession *LPSession = [NSURLSession sessionWithConfiguration:
                                   [NSURLSessionConfiguration defaultSessionConfiguration]];
        [[LPSession downloadTaskWithURL:URL completionHandler: ^(NSURL *temporaryLocation, NSURLResponse *response, NSError *error) {
            if (error) {
                NSLog(@"Leanplum: Error with downloading rich push: %@",[error localizedDescription]);
                self.contentHandler(self.bestAttemptContent);
                return;
            }

            NSString *fileType = [self determineType:[response MIMEType]];
            NSString *fileName = [[temporaryLocation.path lastPathComponent] stringByAppendingString:fileType];
            NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
            [[NSFileManager defaultManager] moveItemAtPath:temporaryLocation.path toPath:temporaryDirectory error:&error];

            NSError *attachmentError = nil;
            UNNotificationAttachment *attachment =
            [UNNotificationAttachment attachmentWithIdentifier:@"" URL:[NSURL fileURLWithPath:temporaryDirectory] options:nil error:&attachmentError];

            if (attachmentError != NULL) {
                NSLog(@"Leanplum: Error with the rich push attachment: %@",
                      [attachmentError localizedDescription]);
                self.contentHandler(self.bestAttemptContent);
                return;
            }
            self.bestAttemptContent.attachments = @[attachment];

            NSLog(@"self.bestAttemptContent.attachments : %@",
                  self.bestAttemptContent.attachments);

            self.contentHandler(self.bestAttemptContent);
            [[NSFileManager defaultManager] removeItemAtPath:temporaryDirectory error:&error];
        }] resume];
    }
}

- (NSString*)determineType:(NSString *) fileType {
    // Determines the file type of the attachment to append to NSURL.
    if ([fileType isEqualToString:@"image/jpeg"]){
        return @".jpg";
    }
    if ([fileType isEqualToString:@"image/gif"]) {
        return @".gif";
    }
    if ([fileType isEqualToString:@"image/png"]) {
        return @".png";
    } else {
        return @".tmp";
    }
}

这篇关于富推送通知不适用于 IOS 中的 FCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:16