ios - iOS 富通知:发送 MP4 文件会导致缩略图透明
问题描述
我刚刚在我的 iOS 应用程序的推送通知中添加了 MP4/GIF 附件。播放方面一切正常。我面临的问题是发送 MP4 视频时,推送中发送的小缩略图看起来是透明的。然而,当我扩展它时,它看起来很完美,我也可以在推内很好地发挥它。当我发送相同的转换为 GIF 的视频时,缩略图看起来也很完美。
这是一个例子:
上面的示例显示了两个不同的应用程序,只是为了显示 MP4 和 GIF 缩略图如何显示同一事件。如果我将 GIF 发送到顶部的应用程序,缩略图的输出看起来与 Pushover 应用程序缩略图完全相同。
这是当我滑动并查看缩略图(透明的)时发生的情况。这个特殊的扩展缩略图用于不同的事件(我丢失了那个旧事件)。但我想说的是,扩展视图看起来很完美。而且演奏也很完美。
所以总而言之,在 IOS 中,当我将 MP4 文件作为附件发送时,小缩略图看起来是透明的,但播放效果很好。扩展缩略图看起来很完美。
这是我的客户代码:
//
// 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:
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 以排除任何格式问题,这里是我上传到谷歌驱动器的一个(链接)。我已经用 ffshow 提取了帧信息,它看起来对我来说并不格格不入(而且它播放得很好)。
有人可以帮我理解为什么最初的小缩略图在 iOS 中看起来很乱吗?(如果有帮助,我在 iOS 13.x 上)谢谢。
解决方案
有同样的问题。通过创建通知内容扩展并将缩略图 URL 与视频 URL 一起发送来解决它。
在服务扩展中,我将视频和缩略图都添加为附件,缩略图作为第一个元素,iOS 将在通知预览中显示:
// 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 的内容扩展处理,我忽略缩略图并只显示视频,这将是最后一个附件。
推荐阅读
- go - 在启用 pkg/profile 的情况下运行 go 时如何获取样本?
- sql - SQL - 试图添加替换的 XML 解析中的非法字符
- pgpool - pgpool-II 3.7.5 不缓存 PG 连接
- html - 透明的 CSS 阶梯式进度条
- azure-devops - 在 Azure DevOps 中编辑现有 sprint 的名称?
- windows - Visual Studio 2017 未检测到 Windows 驱动程序工具包安装
- c# - 构建 C# 控制台应用程序时出错:错误 CS0041 找不到模块
- c - 程序从字符串中删除特殊字符和数字,只打印英文字母
- c# - 将对象序列化为 XML,包括“xsi:schemaLocation”和属性顺序
- cassandra - 延迟复制 Cassandra NoSQL