我试过在这里读类似的文章How to make your push notification Open a certain view controller?
但信息并不完整。
我正在尝试实现推送通知(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
}
}
最佳答案
委托方法
在用户点击显示通知后处理通知消息。
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
//get required values from data
completionHandler();
//push to view controller
}
注意:在项目级解析或保存dictionary对象后,将此dictionary数据保存到模型中,以便在项目中的任何位置使用。
在推到任何视图控制器之前,声明该控制器的字典或模式,以便在那里查看日期。