在Swift 4中,将UNNotificationAction添加到Firebase FCM推送通知是通过以下步骤实现的:
import UserNotifications
import FirebaseMessaging
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册远程通知
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
// Firebase配置
FirebaseApp.configure()
return true
}
// 远程通知注册成功
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
// 远程通知注册失败
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
// 接收到远程通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
// 处理通知内容
}
// iOS 10以上版本接收到远程通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// 处理通知点击事件
}
}
extension AppDelegate {
// iOS 10以上版本显示通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// 处理通知显示
}
// iOS 10以上版本点击通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// 处理通知点击事件
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
// 默认点击事件
} else if response.actionIdentifier == "ActionIdentifier" {
// 自定义点击事件
}
completionHandler()
}
}
{
"to": "device_token",
"notification": {
"title": "Notification Title",
"body": "Notification Body"
},
"data": {
"action": "ActionIdentifier"
}
}
这样,你就可以在Swift 4中将UNNotificationAction添加到Firebase FCM推送通知中了。请注意,以上代码示例中并未提及具体的腾讯云产品,你可以根据实际需求选择适合的腾讯云产品来实现相应的功能。
领取专属 10元无门槛券
手把手带您无忧上云