我正在使用swift3。我想在我的应用收到FCM
消息数据时向UIViewController
中的tableview
显示FCM
消息。我已经在appDelegate
中使用以下代码获得了我的 fcm 数据..但是我不知道如何在UIViewController
中获取数据.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
let message : UNNotificationPresentationOptions = .alert
//GlobalVariables.notification_message = getAlert(notification: .alert)
if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing.
print("active****")
print("(notification.request.content.userInfo)")
completionHandler([message, .sound])
} else {
print("Not active****")
completionHandler([message, .badge, .sound])
}
}
请帮助我提前感谢
在 AppDelegate 上设置一个变量,然后从以下任一位置使用它:
- AppDelegate's didFinishLaunchingWithOptions on
window?.rootViewController
- 通过调用
(UIApplication.shared.delegate as? AppDelegate)?.yourVariable
的视图控制器
我建议在使用 AppDelegate 后将变量设置为 nil,以防止两次使用相同的通知数据。
在应用程序委托中像这样更改代码
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
// FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
}
func applicationWillTerminate(_ application: UIApplication) {
UserDefaults.standard.removeObject(forKey: "check")
UserDefaults.standard.removeObject(forKey: "userid")
UserDefaults.standard.removeObject(forKey: "patient_no")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active{
UIApplication.shared.applicationIconBadgeNumber = 0
if let notification = userInfo["aps"] as? [AnyHashable: Any],
let alert = notification["alert"] as? String {
print(alert)
message = alert
let localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(localNotification)
print("Active----")
}
}else if state == .inactive{
if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String {
print(alert)
message = alert
let localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(localNotification)
print("Inactive----")
}
}else if state == .background{
if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String,let sound = notification["sound"] as? String{
print(alert)
message = alert
var localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = sound
UIApplication.shared.applicationIconBadgeNumber = number!+1
print(number!+1)
UIApplication.shared.scheduleLocalNotification(localNotification)
print("Background----")
}
}
}
然后将其添加到您想要返回消息的位置
let delegate = UIApplication.shared.delegate as! AppDelegate
let message = delegate.message
- 这是一个示例,只有您要添加一些条件