如何在 iOS 10 应用程序中向我的推送通知添加媒体附件?
How can I add media attachments to my push notifications in an iOS 10 app?
有多个示例可以说明如何设置项目以添加使用 'media attachments' 技术显示图像的丰富通知。我已经阅读了其中的大部分,但我错过了一些内容,因为我的项目没有使用此有效负载显示任何丰富的通知(使用 APNS-Tool 和 Boodle 进行测试):
{
"aps":{
"alert":{
"title": "Rich test",
"body": "Dancing Banana"
},
"mutable-content": 1
}
}
通知是可见的,但在 3D Touch 上,没有显示其他信息(例如图像或修改后的标题)。通知扩展断点和 NSLog
消息也不起作用。
这是我的演示项目:https://github.com/gklka/RichTest
我做了什么:
- 创建了一个新项目
- 实施iOS-style授权:
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);
}
}];
- 为 AppDelegate 添加了调试:
- (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 加载图像,而不是加载包。在那种情况下,我很确定您还必须在 NSURL
上使用 startAccessingSecurityScopedResource
(连同 stopAccessingSecurityScopedResource
)。
希望对您有所帮助!
有多个示例可以说明如何设置项目以添加使用 'media attachments' 技术显示图像的丰富通知。我已经阅读了其中的大部分,但我错过了一些内容,因为我的项目没有使用此有效负载显示任何丰富的通知(使用 APNS-Tool 和 Boodle 进行测试):
{
"aps":{
"alert":{
"title": "Rich test",
"body": "Dancing Banana"
},
"mutable-content": 1
}
}
通知是可见的,但在 3D Touch 上,没有显示其他信息(例如图像或修改后的标题)。通知扩展断点和 NSLog
消息也不起作用。
这是我的演示项目:https://github.com/gklka/RichTest
我做了什么:
- 创建了一个新项目
- 实施iOS-style授权:
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); } }];
- 为 AppDelegate 添加了调试:
- (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 加载图像,而不是加载包。在那种情况下,我很确定您还必须在 NSURL
上使用 startAccessingSecurityScopedResource
(连同 stopAccessingSecurityScopedResource
)。
希望对您有所帮助!