gkl*_*lka 9 objective-c push-notification apple-push-notifications ios10
有多个示例,您应该如何设置项目以添加使用"媒体附件"技术来显示图像的丰富通知.我已经阅读了大部分内容,但我错过了一些内容,因为我的项目没有显示任何带有此负载的丰富通知(使用APNS-Tool和Boodle进行测试):
{
"aps":{
"alert":{
"title": "Rich test",
"body": "Dancing Banana"
},
"mutable-content": 1
}
}
Run Code Online (Sandbox Code Playgroud)
通知可见,但在3D Touch上,没有显示其他信息(如图像或修改后的标题).通知扩展断点和NSLog
消息也不起作用.
这是我的演示项目:https://github.com/gklka/RichTest
我做了什么:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"Granted"); [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { NSLog(@"Error registering: %@", error); } }];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"Token: %@", deviceToken); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Error: %@", error); }
添加banana.gif
到项目中
添加了添加香蕉附件的代码 NotificationService.m
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; // Add image attachment NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"banana" withExtension:@"gif"]; NSError *error; UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"banana" URL:fileURL options:nil error:&error]; self.bestAttemptContent.attachments = @[attachment];
我错过了什么?
附加信息:当我使用本地通知而不是远程通知时,同样的事情.
你大部分时间都到了那里。发送附件的方式通常是作为负载中的 URL。但是,如果您想对附件进行硬编码,就像您的代码一样,我认为它会起作用,但我认为您错过了一个关键组件。我认为服务扩展无法访问主包或其资源。如果您将资源添加到服务扩展并尝试加载该资源(使用类似的东西[NSBundle bundleForClass:[NotificationService class]]
),我怀疑它会起作用。
但是,如果您将 URL 作为负载的一部分发送,那么您将从该 URL 加载图像,而不是捆绑包。在这种情况下,我很确定您还必须使用startAccessingSecurityScopedResource
on NSURL
(与stopAccessingSecurityScopedResource
)。
希望有帮助!
归档时间: |
|
查看次数: |
3430 次 |
最近记录: |