skip to Main Content

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)

image

2

Answers


  1. 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

    Login or Signup to reply.
  2. What you call notification=messaging.Notification() there, should be called data={}

    The example looks whole different:

    # The topic name can be optionally prefixed with "/topics/".
    topic = 'highScores'
    
    # See documentation on defining a message payload.
    message = messaging.Message(
        data={
            'score': '850',
            'time': '2:45',
        },
        topic=topic,
    )
    
    # Send a message to the devices subscribed to the provided topic.
    response = messaging.send(message)
    
    # Response is a message ID string.
    print('Successfully sent message:', response)
    

    https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-topics

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search