>我试图在这里阅读类似的线程 如何使您的推送通知打开某个视图控制器?但信息并不完整。
我正在尝试实现推送通知(firebase cloud messaging
(。 收到通知警报后,我希望如果用户点击该通知,它将用户发送到某个视图控制器并打开从服务器发送的数据。
- 如何从通过推送通知发送的服务器访问数据/信息?
- 将该数据/信息发送到某个视图控制器?
这是我在应用程序委托中的代码
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var fcmTokenUser : String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)
// To get FCM token that will be sent to APNS via Google FCM
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
fcmTokenUser = token
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
// This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.
// after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)
fcmTokenUser = fcmToken
}
private func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
}
代码添加到您的AppDelegate
以检测用户tap
(响应(,双击后,它将显示您指定的特定viewController
。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//THIS PRINT WILL SHOW YOU THE USER TAP ON NOTIFICATION
print("userNotificationCenter --- (response) --- ")
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "yourViewControllerIdentifier") as! yourViewControllerClassName
window?.rootViewController = otherVC;
}
要从通过push notification
发送的服务器访问data/information
-
通知数据以 application:didReceiveRemoteNotification:
的形式传递到您的应用程序。如果要在applicationDidBecomeActive:
中处理它,则应将其存储在application:didReceiveRemoteNotification:
中,然后在applicationDidBecomeActive.
中再次读取它
委托方法以 在用户点击显示通知后处理通知消息。
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
//get required values from data
completionHandler();
//push to view controller
}
注意:解析后将此字典数据保存在模型中,或在项目级别保存字典对象,以便在项目中的任何位置使用。比之前推送到任何视图控制器声明字典或模态该控制器,您希望以哪种方式查看日期。