Eir*_*eia 6 ios firebase swift firebase-cloud-messaging
大家好,我读到了人们在使用 Firebase 和 iOS 通知时遇到的一些问题,但似乎有人遇到了同样的问题。
现在我正在谈论FOREGROUND应用程序状态:
应用程序收到通知,例如这样的参数通知:
let notificationsParameters:[String:AnyObject] = [
"to": "iphoneID",
"content-available":true,
"priority":"high",
// "notification" : [
// "body" : "great match!",
// "title" : "Portugal vs. Denmark",
// "icon" : "myicon"
// ],
"data" : [
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
]
]
Run Code Online (Sandbox Code Playgroud)
但是一旦我删除了通知部分,应用程序就什么也没有收到,即使保持在前台,我看到每个人都应该收到通知。
我为接收甚至关闭/背景和内容可用添加了高优先级,我阅读的内容应该可以解决我的问题,但事实并非如此。
在这里你有涉及的代码:
应用程序委托
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey(Constants.GoogleMaps.APIKey)
FIRApp.configure()
/* NOTFICATIONS */
let notificationsType:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let notificationSettings = UIUserNotificationSettings(forTypes: notificationsType, categories: nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification(_:)), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
Run Code Online (Sandbox Code Playgroud)
通知(在应用程序委托中)
据我了解,这就是我将收到通知数据的假设
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
func tokenRefreshNotification(notification:NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
connectToFCM()
}
func connectToFCM() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if error != nil {
print("GMS ERROR: \(error)")
}
else {
print("Connected to GMS")
}
}
}
Run Code Online (Sandbox Code Playgroud)
调用 Firebase 通知
func sendNotification() {
let notificationsParameters:[String:AnyObject] = [
"to": "iphoneID",
"content-available":true,
"priority":"high",
// "notification" : [
// "body" : "great match!",
// "title" : "Portugal vs. Denmark",
// "icon" : "myicon"
// ],
"data" : [
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
]
]
let URL = NSURL(string: "https://fcm.googleapis.com/fcm/send")!
let URLRequest = NSMutableURLRequest(URL: URL)
URLRequest.HTTPMethod = "POST"
URLRequest.setValue("key=\(Constants.Firebase.NotificationsKey)", forHTTPHeaderField: "Authorization")
URLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let encoding = Alamofire.ParameterEncoding.JSON
let encoded = encoding.encode(URLRequest, parameters: (notificationsParameters)).0
Alamofire.request(encoded)
}
Run Code Online (Sandbox Code Playgroud)
您需要任何进一步的信息,请告诉我!
您的方向是正确的,但content_available
参数中需要下划线而不是连字符。notification
如果您希望使用 FCM 获得仅数据(静默)通知,则无需使用该对象。
例如,在您的 FCM 发送请求正文中:
{
"to": "iPhoneID",
"content_available": true,
"priority": "high",
"data": {
"nick": "mario",
"room": "PortugalVSDenmark"
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些和我有同样问题的人,一种解决方案是设置通知参数,如下所示:
let notificationsParameters:[String:AnyObject] = [
"to": "iphoneID",
"content-available":true,
"priority":"high",
"notification" : [
"sound" : " "
],
"data" : [
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
]
]
Run Code Online (Sandbox Code Playgroud)
当应用程序被杀死时,您不会收到通知,但在前台和后台时您将只能收到数据通知,祝您好运!