【问题标题】:How can I add media attachments to my push notifications in an iOS 10 app?如何在 iOS 10 应用程序中将媒体附件添加到我的推送通知?
【发布时间】:2017-02-10 20:28:30
【问题描述】:
有多个示例如何设置项目以添加使用“媒体附件”技术显示图像的丰富通知。我已经阅读了其中的大部分内容,但我错过了一些内容,因为我的项目没有显示任何带有此有效负载的丰富通知(使用 APNS-Tool 和 Boodle 测试):
{
"aps":{
"alert":{
"title": "Rich test",
"body": "Dancing Banana"
},
"mutable-content": 1
}
}
通知是可见的,但在 3D Touch 上,没有显示其他信息(如图像或修改后的标题)。通知扩展断点和NSLog 消息也不起作用。
这是我的演示项目:https://github.com/gklka/RichTest
我做了什么:
- 创建了一个新项目
- 实现了 iOS 风格的授权:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[中心 requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL 已授予, NSError * _Nullable 错误) {
如果(授予){
NSLog(@"授予");
[[UIApplication sharedApplication] registerForRemoteNotifications];
} 别的 {
NSLog(@"注册错误:%@", error);
}
}];
- 为 AppDelegate 添加了调试:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Token: %@", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"错误:%@", 错误);
}
- 为项目添加了一个新目标:通知服务扩展:
在项目中添加了banana.gif
添加了将香蕉附件添加到NotificationService.m的代码
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
// 添加图片附件
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"banana" withExtension:@"gif"];
NSError *错误;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"banana" URL:fileURL options:nil error:&error];
self.bestAttemptContent.attachments = @[附件];
我错过了什么?
附加信息:当我使用本地通知而不是远程通知时,同样的效果。
【问题讨论】:
标签:
objective-c
push-notification
apple-push-notifications
ios10