【问题标题】:UserNotification ios 10用户通知 iOS 10
【发布时间】:2016-10-03 08:58:04
【问题描述】:

我想在 UserNotification(本地通知)中显示 gif 动画,但我只能在用户通知中显示图像,动画不会显示。我在objective-c中这样做

我的代码 //安排一个通知

NSString *UNExtensionID=@"animatedContentExtension";
NSError *error;
UNMutableNotificationContent *content = [[UNMutableNotificationContent    alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Test   Notification:" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello  there!It is a multimedia notification with animation!"
                                                     arguments:nil];
content.sound = [UNNotificationSound defaultSound];

UNNotificationAttachment *attachment;
 NSURL *url =  [[NSBundle mainBundle] URLForResource:@"bike" withExtension:@"png"];

 attachment=[UNNotificationAttachment attachmentWithIdentifier:@"imageID"
                                              URL: url
                                            options:nil
                                            error:&error];
content.attachments=@[attachment];
content.categoryIdentifier=UNExtensionID;

content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
 UNTimeIntervalNotificationTrigger *trigger =    [UNTimeIntervalNotificationTrigger
                                              triggerWithTimeInterval:20.0f repeats:NO];
  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                      content:content trigger:trigger];


UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (!error) {
        NSLog(@"add NotificationRequest succeeded!");
    }
}];

// AppDelegate 中的这段代码 - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler{

 UNNotificationAttachment *attachment;
 NSError *error;

  UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
 NSURL *url =  [[NSBundle mainBundle] URLForResource:@"bike" withExtension:@"png"];

 attachment=[UNNotificationAttachment attachmentWithIdentifier:@"imageID"
                                                          URL:url
                                                      options:nil
                                                        error:&error];
    content=request.content.mutableCopy;
    content.attachments= @[attachment];
    `enter code here`contentHandler(content);

}

【问题讨论】:

  • 你读过文档吗?
  • 你必须提供这样的 gif 图片, NSURL *url = [[NSBundle mainBundle] URLForResource:@"recording_animate" withExtension:@"gif"]; @kishan

标签: objective-c ios10


【解决方案1】:

您必须将您的.gif 文件添加到Notification Service Extension 文件中。 您可以从中创建一个UNNotificationAttachment 对象并传入UNNotificationServiceExtension 子类的func didReceiveNotificationRequest(request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) 方法的completionHandler。

【讨论】:

  • 我不能;不赞成你的回答,因为我的信用低于 15 分抱歉
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2016-11-28
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多