Jay*_*kay 2 apple-push-notifications rich-notifications
我需要在 iOS 10 及更高版本上为 iPhone(5x、6x 和 7x)型号启用丰富的通知。通知带有嵌入的图像,默认情况下应扩展图像。请参阅下面的示例图像:
任何人都可以帮忙吗?
提前致谢。
我在 iOS 10 中的 Rich Notification 文档中创建了一个示例,看看它你可能会得到一些关于 Rich Notification 的想法,该示例是关于在 UNNotificationContentExtension
第1步
第 2 步:创建 UNNotificationContentExtension
单击底部的+图标创建目标模板,然后选择通知内容扩展 -> 下一步 -> 为内容扩展创建名称 -> 完成
第三步:配置创建的扩展的info.plist文件
NSExtension 中的字典表示通知内容的显示方式,这些是在长按收到的通知时执行的
self.title = myTitleUNUserNotificationCenter在您的应用程序中创建类别。这里它可以是字符串或字符串数组,因此每个类别都可以提供不同类型的数据,我们可以从中创建不同的 UI。我们发送的有效负载必须包含类别名称才能显示此特定扩展名第 4 步:在我们的应用程序中创建UNNotificationAction和UNNotificationCategory
在您的应用程序的 AppDelegate.swiftdidFinishLaunchingWithOptions函数中添加
let userNotificationAction:UNNotificationAction = UNNotificationAction.init(identifier: "ID1", title: "???????", options: .destructive)
let userNotificationAction2:UNNotificationAction = UNNotificationAction.init(identifier: "ID2", title: "Success", options: .destructive)
let notifCategory:UNNotificationCategory = UNNotificationCategory.init(identifier: "CATID1", actions: [userNotificationAction,userNotificationAction2], intentIdentifiers: ["ID1","ID2"] , options:.customDismissAction)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().setNotificationCategories([notifCategory])
UIApplication.shared.registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)
我们创建了两个UNNotificationAction使用标识符ID1和ID2并添加这些动作到UNNotificationCategory与标识符CATID1(该在的categoryID ContentExtension的Info.plist文件是一样的,我们在这里创建了应该有效载荷和plist文件中使用)。我们将类别设置为我们的应用程序,UNUserNotificationCenter并在下一行注册通知,该通知调用didRegisterForRemoteNotificationsWithDeviceToken我们获取设备令牌的函数
注意:不要忘记import UserNotifications在你的 AppDelegate.swift 中添加UNUserNotificationCenterDelegate
步骤 5:NotificationContent 的示例负载
'aps': {
'badge': 0,
'alert': {
'title': "Rich Notification",
'body': "Body of RICH NOTIFICATION",
},
'sound' : "default",
'category': "CATID1",
'mutable-content':"1",
},
'attachment': "2"
Run Code Online (Sandbox Code Playgroud)
第 6 步:配置 ContentExtension
执行通知操作时,将自动显示该类别的相应操作。让我们看看代码是如何执行的
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var imageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
}
func didReceive(_ notification: UNNotification) {
self.title = "Koushik"
imageView?.backgroundColor = UIColor.clear
imageView?.image = #imageLiteral(resourceName: "welcome.jpeg")
}
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
self.title = "Koushik"
imageView?.image = UIImage.init(named: "Success.jpeg")
if(response.actionIdentifier == "ID1")
{
imageView?.image = UIImage.init(named: "Success.jpeg")
}
else
{
imageView?.image = UIImage.init(named: "welcome.jpeg")
}
}
}
Run Code Online (Sandbox Code Playgroud)
第 7 步:结果
标题是“Koushik”,因为我们给出了self.title = "Koushik"并且UNNotificationExtensionOverrideDefaultTitle是 YES。在第 3 步中UNNotificationExtensionDefaultContentHidden,如果是“是”,我们给出“否”,那么通知将看起来像图 3 和图 4。
注意:我们不能在内容扩展中使用滚动视图或任何类型的滚动,但我们可以self.preferredContentSize = CGSize(width: 280, height: minimumSize.height)用来增加视图的内容大小但默认的消息应用程序使用滚动。如果我错了,请纠正我。
| 归档时间: |
|
| 查看次数: |
1647 次 |
| 最近记录: |