0

What I Have:

  1. A Python script which sends data-only notification messages to my device/emulator using firebase_admin.messaging.send
  2. A Flutter mobile app which listens for the incoming messages (FirebaseMessaging.onMessage.listen and FirebaseMessaging.onBackgroundMessage)
  3. The data message is parsed and a push notification is shown using flutter_local_notifications

The issue:

The notifications don't work on just iOS. Specifically, the firebase message handlers (FirebaseMessaging.onMessage.listen and FirebaseMessaging.onBackgroundMessage) don't fire for iOS when I send a notification message from my backend.

What I Tried / Things That Do Work:

  1. On Android, the entire process works
  2. APNs token prints successfully from Flutter
  3. On my iOS device, sending a test notification from Firebase Admin Console works, so I'm guessing APNs is setup correctly
  4. On my iOS device, local push notifications work when I trigger one from main() at startup (manually using FlutterLocalNotificationsPlugin.show)
  5. On my iOS device, while the Flutter handlers don't fire, didReceiveRemoteNotification in my AppDelegate.swift does fire and prints the userInfo logging statement in XCode

I have no clue how to resolve this or debug further - any ideas?

Code Snippets:

Permissions

// Info.plist contains
...
    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
    <key>FirebaseScreenReportingEnabled</key>
    <true/>
    <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
        <string>fetch</string>
    </array>
...
//AppDelegate.swift

import Flutter
import UIKit
import Firebase
import FirebaseMessaging
import flutter_local_notifications

@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
    GeneratedPluginRegistrant.register(with: registry)}
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)

    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   Messaging.messaging().apnsToken = deviceToken
   super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
  }

  override func application(_ application: UIApplication,
    didReceiveRemoteNotification userInfo: [AnyHashable: Any],
    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("Received remote notification: \(userInfo)") // <--- THIS PRINTS CORRECTLY
    // Forward the notification to Firebase Messaging
    Messaging.messaging().appDidReceiveMessage(userInfo)
    
    // Inform iOS that processing is complete
    completionHandler(.newData)
  }
}
# backend python script notification function

import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging

def send_push_notification(token, data):
    message = messaging.Message(
        token = token,
        data = data,
        apns=messaging.APNSConfig(
            headers={"apns-priority": "5"},
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    content_available=True
                )   
            )   
        )   
    )   

    logger.info('Sending push notification: (%s)' % (token))
    logger.info(data)

    try:
        response = messaging.send(message)
        logger.info('Successfully sent message')
    except Exception as e:
        logger.info(f'Error sending message: {e}')

3
  • I'm not sure if data-only messages can show a notification on iOS when the app is forcefully closed. See for example: stackoverflow.com/questions/69007744/… Commented May 21 at 19:49
  • @FrankvanPuffelen I don't show the data-only messages. I use the info in the message to show a local push notification. And it doesn't work regardless of whether the app is open/closed. Commented May 21 at 20:36
  • @FrankvanPuffelen While I'm not sure why data only messages don't fire the on message callbacks regardless of whether the app is open/closed, I worked around my issue by sending notification messages instead of data only messages based on your suggestion. I only show the local notification for when the message is received in the foreground to achieve the same behavior. Commented May 21 at 21:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.