Hey everyone I've reading some problems that people have with Firebase and iOS notifications but seems that anybody is having the same issue.
For now I'm talking on FOREGROUND app state:
App receive the notification ever that have the parameter notification for exemple like this:
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"
]
]
but once I removed the notification part app doesn't receive nothing, even keeping in foreground where as I see everyone should receive the notification.
I added priority high to receiving even closed/background and content-available, that I read is suppoused to solve my problem but is not the case.
Here you have the involved code:
APP DELEGATE
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
}
NOTIFICATIONS (IN APP DELEGATE)
As I understand this is what is suppouse that I will receive the data of the notification
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")
}
}
}
CALL TO FIREBASE NOTIFICATIONS
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)
}
Any further information you need just let me know!
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.
You can send up to 240 messages/minute and 5,000 messages/hour to a single device.
public class FirebaseMessagingService extends Service. Base class for receiving messages from Firebase Cloud Messaging. Extending this class is required to be able to handle downstream messages.
The FCM Web interface uses Web credentials called "Voluntary Application Server Identification," or "VAPID" keys, to authorize send requests to supported web push services. To subscribe your app to push notifications, you need to associate a pair of keys with your Firebase project.
You were on the right track, but need an underscore instead of hyphen in the content_available
parameter. No need to use the notification
object if you want a data only (silent) notification using FCM.
Eg, in your FCM send request body:
{
"to": "iPhoneID",
"content_available": true,
"priority": "high",
"data": {
"nick": "mario",
"room": "PortugalVSDenmark"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With