首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS丰富通知:发送MP4文件会生成透明的缩略图

问iOS丰富通知:发送MP4文件会生成透明的缩略图
EN

Stack Overflow用户
提问于 2020-03-23 19:41:34
回答 1查看 469关注 0票数 2

我刚刚在我的iOS应用程序的推送通知中添加了MP4/GIF附件。就回放而言,一切都很正常。我面临的问题是,当发送MP4视频时,推送中发送的小缩略图看起来是透明的。然而,当我扩展它时,它看起来很完美,我也可以在推送中玩得很好。当我发送同样的视频转换为GIF,缩略图也看起来很完美。

下面是一个示例:

​

​

上面的例子展示了两个不同的应用程序,只是为了展示MP4和GIF缩略图是如何在同一个活动中出现的。如果我将GIF发送到顶部的应用程序,缩略图的输出看起来与Pushover应用程序的缩略图一模一样。

这是当我滑动并查看缩略图(透明的)时发生的事情。这个特殊的扩展缩略图用于不同的事件(我失去了那个旧事件)。但我想说的是,展开后的视图看起来很完美。而且玩得也很完美。

​

​

所以总而言之,在IOS中,当我以附件的形式发送MP4文件时,小的缩略图看起来是透明的,但播放效果很好。展开的缩略图看起来很完美。

这是我的客户端代码:

代码语言:javascript
复制
    //
//  NotificationService.m
//  NotificationService
//
//  
//
//
// Credit https://github.com/Leanplum/Leanplum-iOS-Samples/blob/master/iOS_basicSetup/basicSetup/richPushExtension/NotificationService.m

#import "NotificationService.h"

@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];
    NSDictionary *userInfo = request.content.userInfo;


    // If there is no image in the payload than
    // the code will still show the push notification.
    if (userInfo == nil || userInfo[@"image_url_jpg"] == nil) {
        NSLog(@"zmNinja Notification: Did not get a payload or image");
        [self contentComplete];
        return;
    }

    NSString *mediaUrl = userInfo[@"image_url_jpg"];
   // if (mediaType == nil) {
   //   NSLog(@"zmNinja Notification: No media type specified, assuming .jpg");
  //    mediaType = @".jpg";
  //  }

    // load the attachment
    [self loadAttachmentForUrlString:mediaUrl

                   completionHandler:^(UNNotificationAttachment *attachment) {
                       if (attachment) {
                           self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
                       }
                       [self contentComplete];
                   }];

}

- (NSString*)determineType:(NSString *) fileType {
    // Determines the file type of the attachment to append to NSURL.
    //return @".gif";
      // Determines the file type of the attachment to append to NSURL.
    NSLog (@"zmNinja Notification: determineType got filetype=%@",fileType);
    if ([fileType isEqualToString:@"image/jpeg"]){
        NSLog (@"zmNinja Notification: returning JPG");
        return @".jpg";
    }
    if ([fileType isEqualToString:@"video/mp4"]){
        NSLog (@"zmNinja Notification: returning MP4");
        return @".mp4";
    }

    if ([fileType isEqualToString:@"image/gif"]) {
         NSLog (@"zmNinja Notification: returning GIF");
        return @".gif";
    }
    if ([fileType isEqualToString:@"image/png"]) {
         NSLog (@"zmNinja Notification: returning PNG");
        return @".png";

    }
     NSLog (@"zmNinja Notification: unrecognized filetype, returning JPG");
    return @".jpg";


}

- (void)loadAttachmentForUrlString:(NSString *)urlString 
                 completionHandler:(void(^)(UNNotificationAttachment *))completionHandler  {

    __block UNNotificationAttachment *attachment = nil;
    NSURL *attachmentURL = [NSURL URLWithString:urlString];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session downloadTaskWithURL:attachmentURL
                completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
                    if (error != nil) {

                        NSLog(@"unable to add attachment: %@", error.localizedDescription);

                    } else {
                        NSString *fileType = [self determineType: [response MIMEType]];
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileType]];
                        [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];

                        NSError *attachmentError = nil;
                        attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
                        if (attachmentError) {

                            NSLog(@"unable to add attchment: %@", attachmentError.localizedDescription);

                        }
                    }
                    completionHandler(attachment);
                }] resume];
}

- (void)contentComplete {
    self.contentHandler(self.bestAttemptContent);
}

- (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.
    NSLog (@"zmNinja Notification: Time about to expire, handing off to best attempt");
    self.contentHandler(self.bestAttemptContent);
}

@end

服务器端使用FCM传统API:

代码语言:javascript
复制
my $ios_message = {
    to           => $obj->{token},
    notification => {
      title => $title,
      body  => $body,
      sound => "default",
      badge => $badge,
    },
    data => {
      myMessageId => $notId,
      mid         => $mid,
      eid         => $eid,
      summaryText => $eid
    }
  };
  $ios_message->{data}->{image_url_jpg} = $pic; # $pic is a URL for the mp4
  # image_url_jpg is just a field name. It was originally meant for static images
  # haven't changed it yet, as you see in client code above, it uses that field.
  $json = encode_json($ios_message);
  my $req = HTTP::Request->new( 'POST', $uri );
  $req->header(
    'Content-Type'  => 'application/json',
    'Authorization' => $key
  );
  $req->content($json);
  my $lwp = LWP::UserAgent->new(%ssl_push_opts);
  my $res = $lwp->request($req);

最后,如果你想看一个样例MP4来排除任何格式问题,这是我上传到google drive (link)的一个例子。我已经用ffshow提取了帧信息,它对我来说并不是不合适的(而且它播放得很好)。

有没有人能告诉我为什么最初的小缩略图在iOS中看起来乱七八糟?(如果有帮助,我在iOS 13.x上)谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-11-19 22:14:51

也有同样的问题。通过创建notification content extension并将缩略图地址与视频地址一起发送解决了此问题。

在服务扩展中,我添加了视频和缩略图作为附件,缩略图作为第一个元素,iOS将在通知预览中显示该元素:

代码语言:javascript
复制
// mediaAttachment and thumbnailAttachment are UNNotificationAttachments 
// that have just been downloaded
if let mediaAttachment = mediaAttachment {
    mutableContent.attachments = [mediaAttachment]
}
if let thumbnailAttachment = thumbnailAttachment {
    mutableContent.attachments.insert(thumbnailAttachment, at: 0)
}
contentHandler(mutableContent)

扩展的通知UI由替换默认UI的内容扩展来处理,我忽略了缩略图,只显示视频,这将是最后一个附件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60812895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档