2016-10-03 82 views
1

我想在UserNotification(本地通知)中显示gif动画,但我只能在用户通知中显示图像,动画不会显示。 (UNNotificationRequest *)要求 withContentHandler::(无效(无效)didReceiveNotificationRequest - 我在Objective-CUserNotification ios 10

我的代码 //调度的通知

NSString *[email protected]"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]; 
[email protected][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中 这样做(^)(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); 

}

+0

您是否阅读过文档,也许? – holex

+0

你必须提供这样的gif图像,NSURL * url = [[NSBundle mainBundle] URLForResource:@“recording_animate”withExtension:@“gif”]; @kishan – KAR

回答

1

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

+0

非常感谢:) – kishan

+0

我可以; t upvote您的答案,因为我的信用度低于15对不起 – kishan

+0

您可以标记为正确答案。 – toofani