UserNotification ios 10

时间:2016-10-03 08:58:04

标签: objective-c ios10

我想在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 *)请求                    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);

}

1 个答案:

答案 0 :(得分:1)

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