通知服务扩展在将远程通知传递给用户之前修改其内容。例如,如果远程通知包含图像 URL,则可以使用通知服务扩展来获取图像并将其显示在通知内容中。
但是如何访问通知服务扩展中的实际应用程序代码?假设我在应用程序代码中有一个swift 类,我可以在通知服务扩展DataAccess中访问该类吗?
编辑:有些人建议将应用程序代码添加到服务扩展中,这不适用于我的情况。
ios swift remote-notifications unnotificationserviceextension
我没有获得有关 Notification Service Extension 的配置文件的正确文档。但据我所知,Notification Service Extension 有自己的 bundle id,必须使用自己的 App ID 和配置文件进行设置。
请清除这个疑问,我是否需要使用分发配置文件来扩展通知服务?
xcode push-notification ios provisioning-profile unnotificationserviceextension
我在我的应用程序中启用了 NotificationServiceExtension。并做了以下
以下是我的 NotificationService 的didReceiveNotificationRequest方法
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.badge = @20;
self.contentHandler(self.bestAttemptContent);
Run Code Online (Sandbox Code Playgroud)
}
不知道我在这里缺少什么。搜索并关注了这么多文件。似乎没有任何工作。
我正在使用 XCode 11.2.1 和带有 ios 版本 13 的真实设备。
我的 macOS 应用程序有一个通知服务扩展。
这是该扩展的代码:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
print("Extension received notification!")
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
bestAttemptContent?.title = "Title modified!"
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push …Run Code Online (Sandbox Code Playgroud) 我正在使用通知服务扩展通过推送接收聊天消息。以前我使用 VOIP 发送聊天消息。但是随着 iOS 13 的新更新限制了 VOIP 的使用,我转向了 Silent Push。但是当应用程序处于终止状态时,我无法处理静默推送。
所以我已经转移到通知服务扩展。除了静音聊天功能外,一切都很顺利。我想接收推送,但如果聊天静音,则不应显示。我知道在通知服务扩展中,我们不能跳过它。但无论如何,WhatsApp 正在做我正在寻找的东西......我不知道如何。
即使处于终止状态,有没有办法处理 iOS 13 的静音聊天功能????
voip push-notification ios unnotificationserviceextension ios13
我遇到了一个奇怪的问题。iOS 通知服务扩展将从设备中删除附件。
我使用 SDWebImage 来显示和缓存图像,并实现了通知服务扩展以在通知警报视图中显示图像。
就我而言,图像已在本地缓存。然后,我单击主页按钮,我的应用程序在后台运行,应用程序安排了一个本地通知,并将缓存的图像附加到通知内容中。
请参阅下面的代码:
1.安排本地通知
+ (void)postLocalNotificationGreaterThanOrEqualToiOS10:(LNotification)module body:(NSDictionary *)body {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.sound = [UNNotificationSound defaultSound];
content.body = @"body";
content.userInfo = @{};
//get the image in device to attach into notification
NSError *error;
NSString* imgURL = [body valueForKey:kLocalNotification_Image];
NSString *filePath = [[SDImageCache sharedImageCache] defaultCachePathForKey:imgURL];
NSURL *url = [NSURL URLWithString:[@"file://" stringByAppendingString:filePath]];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"Image" URL:url options:nil error:&error];
if (attachment) {
content.attachments = @[attachment];
}
UNTimeIntervalNotificationTrigger* trigger = …Run Code Online (Sandbox Code Playgroud) 直到最近(我相信在 iOS 12 发布之前),使用removeDeliveredNotifications.
突然,通知服务扩展中没有任何代码更改,通知不再被删除。
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.content = request.content.mutableCopy() as? UNMutableNotificationContent
guard let content = content else {
contentHandler(request.content)
return
}
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
let matchingNotifications = notifications.filter({ $0.request.content.threadIdentifier == "myThread" && $0.request.content.categoryIdentifier == "myCategory" })
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: matchingNotifications.map({ $0.request.identifier }))
contentHandler(content)
}
}
Run Code Online (Sandbox Code Playgroud)
该功能仅完成而不删除通知。在真实设备上调试时,它显示matchingNotifications包含通知并且正确提供了要删除的通知 ID。
对于测试,调用removeAllDeliveredNotifications()有效并删除所有通知。
上面的函数被调用 override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
这里有什么问题?
ios unusernotificationcenter unusernotification unnotificationserviceextension
在我的场景中,我在父 iOS 应用程序和通知服务扩展之间共享数据。我使用的是 Xcode 10.2.1,iOS 部署目标 10.0
我们已经尝试了 NSUserDefaults 和 Keychain 组,它按预期工作。将值(存储模型或数据类型)从通知服务扩展(TargetB)保存到 MainApp(TargetA)的任何其他方式。
一旦应用程序处于终止状态,我们就将值附加到模型中,并将其保存在钥匙串中。
保存到 Keycahin:
NotificationAPI.shared.NotificationInstance.append(Notifications(title: messageTitle, message: messageBody,date: Date()))
let notification = NotificationAPI.shared.NotificationInstance
let value = keychain.set(try! PropertyListEncoder().encode(notification), forKey: "Notification")
Run Code Online (Sandbox Code Playgroud)
对于用户默认:
var defaults = NSUserDefaults(suiteName: "group.yourappgroup.example")
Run Code Online (Sandbox Code Playgroud)
当应用程序处于非活动状态时,我想将数据从目标 B 传输到目标 A?另一个传输或保存数据?请帮我?
我能够在模拟器中测试普通通知,但是当我尝试测试丰富的通知时,没有任何反应,事件标题没有更新。
你能帮我吗,如何继续。我需要更改任何模拟器设置吗?我正在使用 Xcode 11.4
示例有效负载:
{
"aps": {
"mutable-content": 1,
"alert": {
"body": "Push notification body",
"title": "Push notification title"
}
},
"media-url": "https://i.imgur.com/t4WGJQx.jpg"
}
Run Code Online (Sandbox Code Playgroud)
通知服务扩展方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]",
self.bestAttemptContent.title];
}
Run Code Online (Sandbox Code Playgroud) xcode apple-push-notifications ios-simulator unnotificationserviceextension xcode11.4
\n
\n如标题所述,我目前无法从 UNNotificationServiceExtension 中安排任务。我想在调用以下函数并收到推送时安排任务:
func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
目前,只要我打电话:
\n\ntry BGTaskScheduler.shared.submit(request)
输入 catch 子句并返回以下错误:\'操作无法\xe2\x80\x99t 完成。(BGTaskSchedulerErrorDomain 错误 1。)\'
\n\n推送消息通过以下网址通过 Firebase 云消息传递发送,并在我的应用程序中完美接收: https: //fcm.googleapis.com/v1/ {项目路径}。我从 POSTMAN 中调用这个 url。我还指定了访问令牌和 json 正文。
\n\n推送消息中的 apns 标头设置为“apns-push-type”:“background”。Payload.aps 具有以下值:“apns-priority”:“5”,“content-available”:1,“mutable-content”:1。我认为这一切正常,因为我验证了这一点。
\n\n奇怪的是,我确实让后台任务在实际应用程序中工作,因此无需从 UNNotificationServiceExtension 中触发它。我尝试这样做是为了检查代码是否真的有效。
\n\n我已经完成了研究,到目前为止我已经尝试了以下操作:
\n 1.我确保 info.plist 中的“允许的后台任务调度程序标识符”填充了正确的任务标识符。我已三次检查该字符串是否在代码中也正确使用。\n
\n 2.在应用程序中启用以下功能:\'后台获取\'、\'远程通知\'、\'后台处理\' .\n
\n 3.在 info.plist 中,支持以下 UIBackgroundMode:\'audio\'、\'fetch\'、\'processing\'、\'remote-notification\' 和 \'voip\'。这些相同的 UIBackgroundMode 也被输入到 UNNotificationServiceExtension info.plist 中(我这样做只是为了确定)。\n
\n 4.我确保该任务在 AppDelegate 的 \'didFinishLaunchingWithOptions\'\n
\n 5中注册. …
我逐步移动以获得丰富的推送通知。他们来了 :
NotificationService didRecieve:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
func failEarly() {
contentHandler(request.content)
}
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let data = request.content.userInfo as? [String: AnyObject] {
// Grab the attachment
// let notificationData = data["data"] as? [String: String]
if let urlString = data["attachment-url"], let fileUrl = URL(string: urlString as! String) {
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) …Run Code Online (Sandbox Code Playgroud) apple-push-notifications ios swift unnotificationserviceextension
unnotificationserviceextension ×11
ios ×9
swift ×4
xcode ×2
ios13 ×1
macos ×1
objective-c ×1
voip ×1
xcode11.4 ×1