[英]how to get FCM message in a UIViewController in Swift3?
I am using swift3. 我正在使用swift3。 I want to show
FCM
message to tableview
in a UIViewController
when My app receive FCM
message data.I have got my fcm data using bellow code in appDelegate
..But I don't know how to get data in UIViewController
. 我想告诉
FCM
消息tableview
在UIViewController
时,我的应用程序接收FCM
消息data.I已使用波纹管代码得到了我的FCM数据appDelegate
..但我不知道如何获取数据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])
}
}
Please help me Thanks advance 请帮助我,谢谢
Set a variable on your AppDelegate, then use it from either: 在您的AppDelegate上设置一个变量,然后从以下任一位置使用它:
window?.rootViewController
window?.rootViewController
上的didFinishLaunchingWithOptions window?.rootViewController
(UIApplication.shared.delegate as? AppDelegate)?.yourVariable
(UIApplication.shared.delegate as? AppDelegate)?.yourVariable
I recommend setting variable in AppDelegate to nil after using it, to prevent using same notification data twice. 我建议在使用完AppDelegate之后将其设置为nil,以防止两次使用相同的通知数据。
In app delegate change your code like this 在应用程序委托中,像这样更改您的代码
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----")
}
}
}
then add this where you want the message back 然后将其添加到您希望消息返回的位置
let delegate = UIApplication.shared.delegate as! AppDelegate
let message = delegate.message
- This is a sample only you want to add some condition
这只是一个样本,您只想添加一些条件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.