我正在我的两个相关应用程序中实现推送通知,到目前为止我能够发送通知、设备到设备以及主题。收到通知后,通知会显示随有效负载发送的 url 处的图像。我的目标是向主题通知添加操作,并且每个主题的操作都不同。 Ej。行动为"shop-promotions"
主题通知(即"buy"
)将不同于“新闻”主题通知(即"play"
)。因此,我想在远程通知的有效负载中发送一个“类别”参数,并使用它来区分不同的传入通知并显示正确的操作集。是否可以在NotificationExtension.swift中定义操作didReceive
或者还必须使用自定义 UI 来显示操作?
在https://www.pluralsight.com/guides/creating-ios-rich-push-notifications https://www.pluralsight.com/guides/creating-ios-rich-push-notifications显示为可能的,但我没有成功。
我将它们声明为:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
print("NotificationService: dide receive called")
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
if let urlString = bestAttemptContent.userInfo["attachment-url"] as? String,
let data = NSData(contentsOf: URL(string:urlString)!) as Data? {
let path = NSTemporaryDirectory() + "attachment"
_ = FileManager.default.createFile(atPath: path, contents: data, attributes: nil)
do {
let file = URL(fileURLWithPath: path)
let attachment = try UNNotificationAttachment(identifier: "attachment", url: file,options:[UNNotificationAttachmentOptionsTypeHintKey : "public.jpeg"])
bestAttemptContent.attachments = [attachment]
} catch {
print(error)
}
// Actions
if let category = bestAttemptContent.userInfo["category"] as? String {
if category == "shop-promotions" {
let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [])
let close = UNNotificationAction(identifier: "close", title: "Close", options: [])
let category = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
}
}
} // else {
// if let contentHandler: ((UNNotificationContent) -> Void) =
// self.notificationContentHandler,
// let content: UNNotificationContent = self.notificationContent {
// contentHandler(content) }
let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [])
let close = UNNotificationAction(identifier: "close", title: "Close", options: [])
let category = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
contentHandler(bestAttemptContent)
}
这是为通知发送的字典:
let postParams: [String : Any] = [
"to": topic,
"notification": [
// "badge" : 1, sendig the badge number, will cause aglitch
"body": body,
"title": title,
"subtitle": subtitle,
"text": "some text",
"sound" : true, // or specify audio name to play
"priority": "high",
"content_available": true,
"mutable_content": true,
"category": "shop-promotions"
],
"data" : [
"attachment-url": dataUrl,
// "media_type":"image",
"productId": productId,
"price": price
]
]
UPDATES:
- 更改了类别声明,将它们放入我调用的静态函数中
didFinishLaunchingWithOptions
检查通知权限后didRegisterForRemoteNotificationsWithDeviceToken
获得令牌后。
- Set
NotificationService
's info.plist
没有任何UNNotificationExtensionCategory
我看到那是在NotificationContent
's info.plist
.
- 尝试过不同的
Target Membership
两者的设置NotificationService.swift
and info.plist
。什么是正确的配置?
仍然处于相同的情况,仅显示通知的图像。
您能看出缺少什么来正确设置吗?
动作函数是这样的:
static func configurePushCategories() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted:Bool, error:Error?) in
if error != nil {
print(error?.localizedDescription as Any)
}
if granted {
print("Permission granted")
} else {
print("Permission not granted")
}
}
// actions
let buy = UNNotificationAction(identifier: "buy", title: "Buy", options: [.foreground])
let play = UNNotificationAction(identifier: "play", title: "Play", options: [.foreground])
let close = UNNotificationAction(identifier: "close", title: "Close", options: [.foreground])
// let shopPromotionsCategory = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
// let fixItPromotionsCategory = UNNotificationCategory(identifier: "fix-it-promotions", actions: [buy,close], intentIdentifiers: ["buy","close"], options: [])
// let cityFixItNewsCategory = UNNotificationCategory(identifier: "city-fix-it-news", actions: [play,close], intentIdentifiers: ["play","close"], options: [])
// let countryFixItNewsCategory = UNNotificationCategory(identifier: "country-fix-it-news", actions: [play,close], intentIdentifiers: ["play","close"], options: [])
let shopPromotionsCategory = UNNotificationCategory(identifier: "shop-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
let fixItPromotionsCategory = UNNotificationCategory(identifier: "fix-it-promotions", actions: [buy,close], intentIdentifiers: [], options: [])
let cityFixItNewsCategory = UNNotificationCategory(identifier: "city-fix-it-news", actions: [play,close], intentIdentifiers: [], options: [])
let countryFixItNewsCategory = UNNotificationCategory(identifier: "country-fix-it-news", actions: [play,close], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([shopPromotionsCategory,fixItPromotionsCategory, cityFixItNewsCategory, countryFixItNewsCategory])
} else {
// Fallback on earlier versions
}
}