FCM Token dosent save in Database when user clicks "Allow" on notifications - swift

I developed a app that is in the App Store right now. However before launch I tested the app with my iPhone and the FCM token got registered in the database. However now as users start to use the app, I can see that FCM token dosen't save in the database? I asked my friend to test it out, and he neither gets the FCM token stored in the database.
And as a result of this, notifications are not displayed (because the function dosen't have a phone to send the notification to).
Can anyone figure this out?
(I have tried to resolve this issue for the past days, cant seem to catch where its gone wrong)
Code in my first ViewController, that users arrive to after signing up.
func registerForRemoteNotifications() {
Messaging.messaging().delegate = self
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)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
if let userUid = Auth.auth().currentUser?.uid {
Database.database().reference().child("profile/" + userUid + "/fcmToken").setValue(fcmToken)
}
}

I noticed that 'FCM token' is only sent to the db when a user is logged in, So the issue is coming when user opens app for first time when he isnt logged in.
Solution is:
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
UserDefaults.standard.setValue("fcmToken", forKey: fcmToken)
}
later on fetch the token from userDefaults
let deviceToken = UserDefaults.standard.value(forKey:"fcmToken") as? String ?? "not found"
to send it to server just after user gets logged in.

Related

Firebase Cloud Messaging on iOS: 'application:didRegisterForRemoteNotificationsWithDeviceToken' must be present, but is never called

I'm adding Firebase Cloud Messaging in an iOS app (iOS 15.2.1 on a 7th Gen iPad), and I am able to get it to work, but have taken a step that the Firebase docs don't specify, and I don't understand why it's working and why it doesn't work when I don't do this.
First, I'm following these Firebase docs. These docs make no mention of needing to have an application:didRegisterForRemoteNotificationsWithDeviceToken method in the App Delegate. However, if I don't have this method present, I consistently get this error spewed to the console whenever I try to access the token with Messaging.messaging().token:
2022-01-31 22:00:57.448319-0800 authNavFirebaseUISample[4755:1363959] 8.10.0 - [Firebase/Messaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID 'XXXXXXXXXXXX'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.
When I do add in this method, I no longer get that error when accessing the token via Messaging.messaging().token. Furthermore, when this method is present, I can successfully send test notifications via the Notification Composer.
What's particularly strange about all of this is that the actual application:didRegisterForRemoteNotificationsWithDeviceToken is never actually invoked. However, its presence is required and without it, I get that error spew and Notification Composer doesn't work.
I've added relevant code snippets below. Any ideas? Thanks!
App Delegate
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
NotificationHelpers.setupRemoteNotifications(application)
return true
}
// NOTE: Why is this needed?!?!
// If this method isn't present, each time we try to retrieve the FCM (Firebase Cloud
// Messaging) token, we get:
// APNS device token not set before retrieving FCM Token for Sender ID 'XXXXXXXXXXXX'. Notifications to
// this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS
// device token is set.
// Adding this method prevents that. However, the method never actually gets executed. (Try putting a
// print statement in there, it won't print.) But if you remove the method, then we get the above error.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
}
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = MySceneDelegate.self
return sceneConfig
}
}
NotificationHelpers
class NotificationHelpers {
static func setupRemoteNotifications(_ application: UIApplication) -> Void {
let delegate = NotificationsDelegate()
// For iOS 10 and above display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = delegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
application.registerForRemoteNotifications()
Messaging.messaging().delegate = delegate
}
}
Token accessing code (invoked from button press in app)
Button("retrieve FCM") {
// Get token here, but really can be from anywhere
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
}
}
}

Swift didReceiveRemoteNotification() Firebase not being called

After reading about this extensively, I cannot find the solution. I am sending cloud messages from Firebase to my ios app, but the method didReceiveRemoteNotification() is not being called at all, not when the app is in foreground and not in background. I tried sending from the firebase console, and also from postman using topics, and both don't work. I have integrated Firebase in the app, and uploaded certificate to Firebase, and I get no errors in the console. Just the notification isn't coming? I also added the capability of cloud messages.
This is what I do (from the Firebase tutorial) in appDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
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
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// 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
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (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
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
Disable Method swizzling and everything should work fine.
According to the documentation https://firebase.google.com/docs/cloud-messaging/ios/client#token-swizzle-disabled
If you have disabled method swizzling, or you are building a SwiftUI app, you'll need to explicitly map your APNs token to the FCM registration token. Implement the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method to retrieve the APNs token, and then set Messaging's apnsToken property
It also mentions here https://firebase.google.com/docs/cloud-messaging/ios/receive#handle_messages_with_method_swizzling_disabled
By default, if you assign your app's app delegate class to the UNUserNotificationCenter and Messaging delegate properties, FCM will swizzle your app delegate class to automatically associate your FCM token with the device's APNs token and pass notification-received events to Analytics. If you explicitly disable method swizzling, if you are building a SwiftUI app, or if you use a separate class for either delegate, you will need to perform both of these tasks manually.
Mapping apnToken and deviceToken is to be done at various places thus it's advised to read the documentation completely and apply the necessary changes.

FCM: Message sent but not received

tried to solve this myself. spent like an hour or so still no result.
Got me old code of a previous project regarding FCM. however the code was and still working on it's app. though i managed to transfer the code to my new project. but it won't work here.
Now i know APN's are weird and complicated. but it's more of a memorized situation for me.
Things i have done:
- Uploaded my personal .p12 to my firebase project
- Enabled "Push Notifications" in app capabilities
- Imported and used UserNotifications framework on appdelegate.swift
Here's how my AppDelegate look like:
import UIKit
import Firebase
import FirebaseFirestore
import StoreKit
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
override init() {
super.init()
FirebaseApp.configure()
// not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Auth.auth().signInAnonymously() { (authResult, error) in
// ...
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
UIApplication.shared.statusBarStyle = .default
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "gateway") as! gatewayViewController
window!.rootViewController = viewController
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// 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
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
// if let messageID = userInfo[gcmMessageIDKey] {
// print("Message ID: \(messageID)")
// }
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (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
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
//if let messageID = userInfo[gcmMessageIDKey] {
// print("Message ID: \(messageID)")
//}//
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
Okay so with this code you get the devices Firebase registration token, i copied my code and used it on Cloud Functions to send a test message, here's how my CF looks like:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = functions.https.onRequest((request, response) => {
var registrationToken = 'f8fWx_sANVM:APA91bEd46drxiBvHLZd5YKVClQr91oubzJKOyXE1LNgxOsi3ihUw31yEJL6prHKm-A83B1N1sr2GOff3P9tUsRNhCpG7_VMRlDUDfthIcwkDUgzKPV5NZtlo6pcpxsvD9ZgYlPqibNp';
var payload = {
notification: {
title: "just published new Word",
body: "Hii",
}
};
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
Okay so far after hitting the helloWorld url, my console gets this:
Successfully sent message: { results: [ { messageId: '0:1537714204565821%b3b8835bb3b8835b' } ],
canonicalRegistrationTokenCount: 0,
failureCount: 0,
successCount: 1,
multicastId: 8154206809408282000 }
Function execution took 60002 ms, finished with status: 'timeout'
Last time on my previous project it took 20ms at it's very best. I still can't figure this out. Your help is greatly appreciated
You're using a HTTP(S) triggered Cloud Function, which means your code must send a response. Since your code doesn't do that, the function runs for 60s and then gets terminated by the Cloud Functions environment. This means you pay for more time than you actually need, so you'll want to fix it.
For example:
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
// See the MessagingDevicesResponse reference documentation for
// the contents of response.
//return console.log("Successfully sent message:", response);
res.status(200).send(response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
This will get rid of the message in the Cloud Functions logs, and ensures you only pay for the time you actually need.

Firebase FCM push notifications stopped working iOS 11.1.1

I am using Firebase FCM to send push notifications from an iOS device. The push notifications did work, until yesterday.
When I now send a push notification, everything shows successful, but nothing is received on the device.
If I send directly via a curl request, this is the response:
{"multicast_id":7815294000653973158,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1510474219556035%ca9ff0f8ca9ff0f8"}]}
If I send from the Notification dashboard on the firebase console, it shows successfully completed.
I have done the following with no success:
Locked in pod for 'FirebaseInstanceID', "2.0.0", as per FCM Push notifications do not work on iOS 11
Generated a new APN key on developer console and replaced the existing key on FCM setup in Firebase
Downloaded a fresh GoogleService-Info.plist and replaced existing
Checked that bundle id's etc all match
Updated firebase pods to latest:
Using Firebase (4.5.0)
Using FirebaseAnalytics (4.0.4)
Using FirebaseAuth (4.3.1)
Using FirebaseCore (4.0.10)
Using FirebaseFirestore (0.9.1)
Using FirebaseInstanceID (2.0.5)
Using FirebaseMessaging (2.0.6)
Turned message swizzling on and off
Setting Messaging.messaging().shouldEstablishDirectChannel = true as per Firebase notifications not working in iOS 11
Deleted this iOS app from firebase console and redone notification setup from the start
Made sure remote notification in Capabilities are still turned on
Restarted my devices
My setup:
I call FirebaseApp.configure() in the AppDelegate file in the override init() method:
override init() {
super.init()
FirebaseApp.configure()
}
In the AppDelegate didFinishLaunchingWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// get push notification token id for user
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()
return true
}
And then I save my token in userdefaults and persist to database later:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
The token generated here I also test with curl as mentioned above and all shows successful.
My GoogleService-Info.plist:
My Info.plist:
Please let me know if any other info is required.
My Android application is still working as before. I am struggling to understand what could change for the iOS app in one day to cause this, or what I perhaps broke in one day:)
Any help would be greatly appreciated.
Ok, fixed it. I disable firebase's message swizzling by adding this to my Info.plist: FirebaseAppDelegateProxyEnabled: NO
Further I removed all firebase messaging delegate methods. And generated my own APN token in didRegisterForRemoteNotificationsWithDeviceToken and setting Messaging.messaging().apnsToken = deviceToken in the didRegisterForRemoteNotificationsWithDeviceToken method once token generated.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
Messaging.messaging().apnsToken = deviceToken
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
Previously I used the firebase swizzling, it appears as if this stopped working for some reason.
As this was a tough experience for me, I would like to post the steps that I would now reccommend to enable iOS client for FCM:
In the Apple Developer console generate your APN and then select APNs. Select Continue and download your APN certificate. Take note of your Key ID.
Then in the firebase console under settings, cloud messaging, upload your APN key, add the key ID and bundle id, do not upload any p12 certificates.
Disable firebase message swizzling by adding this to your Info.plist:
FirebaseAppDelegateProxyEnabled: NO
Then add the following in your AppDelegate.swift file in the didFinishLaunchingWithOptions method:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// get push notification token id for user
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()
return true
}
Since swizzling is disabled you need to take care of generating your APN token and assigning it to the firebase messaging apnsToken. You do this by having the following method in your AppDelegate.swift file:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
Messaging.messaging().apnsToken = deviceToken
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
I then save the token in the userdefaults to later persist to the database see above:
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
You can test that your token is working by copying the token printed out in the console and using the FCM messaging console. Or by using a curl request in the terminal as below. Note you have to replace YOUR_LEGACY_SERVER_KEY with the legacy server key which you can find in the firebase console under settings and cloud messaging and replace YOUR_TOKEN with the token printed in the console (which was generated above):
curl -X "POST" "https://fcm.googleapis.com/fcm/send" \
-H "Authorization: key=YOUR_LEGACY_SERVER_KEY” \
-H "Content-Type: application/json" \
-d $'{
"notification": {
"body": "Testing with direct FCM API",
"title": "Test Message",
"badge": "0",
"sound": "default"
},
"registration_ids": [YOUR_TOKEN]
}'

How to get the Push Notifications displayed in the Notification center

All of these came from just one app I'm currently developing, let's call this app SampleApp
How do I get a list of these notifications from my Phone's Tray
Yes I do know that I may be able to get the notifications via this code
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
print("here are the iOS 10 notifs \(requests)")
}
} else {
let requests = (UIApplication.shared.scheduledLocalNotifications ?? [])
print("here are the NON iOS 10 notifs \(requests)")
}
But the problem with this code is that I can only get notifications which I created offline, and not those coming from the Apple Push Notification Server (APNS)
By created offline I mean, scheduled UILocalNotifications, and since Push Notifications aren't UILocalNotifications I have no idea what to do
Some Questions You May ask
Are these notifications from my app?
Yes
Am I using the didReceiveRemoteNotification on the AppDelegate?
Yes, but that's different.
Does my remote notifications work/come from APNS
Yes.
What does my code for registering remote notifications look like?
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: authOptions, completionHandler: { (_, _) in })
} else {
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Dávid Pásztor's comment partially solved the issue because now I can retrieve the all (push and non push) Notifications displayed in the Notification Center, but only for iOS 10 because the code below is only for iOS 10 devices and above, what about other versions?
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
print(notifications)
}