I am trying to send push notifications from vs code (firebase admin server) to my iOS device using Firebase topics. I followed every step in the Google tutorial and when I run the python code no message is sent to my iOS device and nothing even appears in the Firebase Messaging dashboard and I get this output:
Successfully sent message: projects//messages/
In my swift app, on my real iPhone device (iOS 18) I added the app delegate below and I tested it and a FCM token is successfully generated. After the FCM token is generated in the delegate function, I call Messaging.messaging().subscribe(toTopic: "info") { error in
which is successful and I get no error.
On my python firebase admin server I authenticate with a service json file (I know this works because I can write/read from the database), I’ll include an image below for the permissions on this service account from IAM. Also If I take the FCM token that my iPhone generates and try to send a Message directly to the token then I get this error:
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://fcm.googleapis.com/v1/projects/xbot-fwerge/messages:send firebase_admin._messaging_utils.ThirdPartyAuthError: Auth error from APNS or Web Push Service
I’d appreciate any help getting these firebase push notifications working.
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in }
application.registerForRemoteNotifications()
return true
}
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { }
}
import firebase_admin
from firebase_admin import messaging, credentials
cred = credentials.Certificate("credsAhmed.json")
firebase_admin.initialize_app(cred)
message = messaging.Message(
notification=messaging.Notification(
title = "New Release",
body = "Nike Book 1 The Nightmare Before Christmas",
),
topic = 'info',
)
response = messaging.send(message)
print('Successfully sent message:', response)
2
Answers
The error 401 Client Error suggests that the service account you’re using might not have sufficient permissions to send notifications.
check permission for Firebase Cloud Messaging Admin and Firebase Admin SDK Administrator Service Agent
What you call
notification=messaging.Notification()
there, should be calleddata={}
…The example looks whole different:
https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-topics