PushExpressLib 是一个库,旨在简化 iOS 应用程序中使用 PushExpress 服务和 Firebase Cloud Messaging (FCM) 集成和处理推送通知的过程。
本指南将引导您将 PushExpressLib 集成到您的应用程序中,并处理前台和后台状态下的推送通知。
在开始之前,请确保您的应用程序已配置为接收远程通知
Remote notifications
。使用 CocoaPods 或 Swift Package Manager (SPM) 安装 FirebaseMessaging
。
将 GoogleService-Info.plist
文件添加到您的项目。
打开 AppDelegate
并导入必要的模块
import Foundation
import UIKit
import Firebase
import UserNotifications
import FirebaseMessaging
在 AppDelegate 中配置 Firebase 并注册远程通知
@main
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure() // Initialize Firebase
application.registerForRemoteNotifications() // Register for remote notifications
return true
}
}
遵循 MessagingDelegate 协议并实现处理 token 的方法
@main
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure() // Initialize Firebase
application.registerForRemoteNotifications() // Register for remote notifications
Messaging.messaging().delegate = self // Set AppDelegate as the delegate for FCM
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken // Pass the APNs token to FCM
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error)")
}
}
将 PushExpressLib 添加到您的项目并初始化它
import Foundation
import UIKit
import Firebase
import UserNotifications
import FirebaseMessaging
import PushExpressLib
@main
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure() // Initialize Firebase
application.registerForRemoteNotifications() // Register for remote notifications
Messaging.messaging().delegate = self // Set AppDelegate as the delegate for FCM
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken // Pass the APNs token to FCM
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
PushExpressManager.shared.setNotificationToken(token: fcmToken) // Set notification token
PushExpressManager.shared.initialize(
appId: "20908-1212", // PushExpress App ID
transportType: .fcm, // Notification transport type (FCM)
foreground: true, // Enable foreground notification handling
extId: nil
)
}
验证集成
运行您的应用程序。从 PushExpress 网站发送测试通知。如果一切设置正确,您应该在设备上看到该通知。
添加一个新的 target Notification Service Extension
如果需要,降低操作系统版本(默认情况下,设置的是最新版本)。将 PushExpressLib 添加到扩展的 Frameworks and Libraries 中。编辑 NotificationService 文件
import UserNotifications
import PushExpressLib
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
let notificationServiceManager = NotificationManager()
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
notificationServiceManager.handleNotification(request: request, contentHandler: contentHandler)
}
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 payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
验证后台处理
关闭您的应用程序。从 PushExpress 网站发送测试通知。如果一切设置正确,您应该在设备上看到该通知。
如果应用程序处于 Terminated 状态,则处理通知点击无效。
您已成功配置了在 iOS 应用程序中使用 PushExpressLib 和 Firebase Cloud Messaging 处理推送通知!按照这些说明集成通知并在前台和后台模式下处理它们。
如果您有任何问题或疑虑,请参阅 PushExpressLib 文档或联系支持。