Skip to content

[firebase_messaging]: iOS onBackgroundMessage is not invoked in release mode #13643

@takatomatsumura

Description

@takatomatsumura

Is there an existing issue for this?

  • I have searched the existing issues.

Are you aware of the differences between iOS and Android background message handling?

  • I understand that iOS and Android background messages behave differently, and I've designed my application with that in mind.

Do you have an active Apple Developer account?

  • I have an active Apple Developer account.

Are you using a physical iOS device to test background messages?

  • I am using a physical iOS device to test background messages.

Have you enabled "Remote Notifications" & "Background Mode" (Checking options for "Background Processing" & "Remote Notifications") in your app's Xcode project?

スクリーンショット 2024-11-03 2 38 05

Have you created an APNs key in your Apple Developer account & uploaded this APNs key to your Firebase console?

console firebase google com_project_settings_cloudmessaging

Have you disabled method swizzling for Firebase in your app?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CADisableMinimumFrameDurationOnPhone</key>
	<true/>
	<key>CFBundleDevelopmentRegion</key>
	<string>$(DEVELOPMENT_LANGUAGE)</string>
	<key>CFBundleDisplayName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleLocalizations</key>
	<array>
		<string>ja</string>
	</array>
	<key>CFBundleName</key>
	<string>Example</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>$(FLUTTER_BUILD_NAME)</string>
	<key>CFBundleSignature</key>
	<string>com.example</string>
	<key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>example</string>
			</array>
		</dict>
	</array>
	<key>CFBundleVersion</key>
	<string>$(FLUTTER_BUILD_NUMBER)</string>
	<key>FirebaseAppDelegateProxyEnabled</key>
	<string>YES</string>
	<key>LSApplicationQueriesSchemes</key>
	<array>
		<string>https</string>
		<string>http</string>
	</array>
	<key>LSRequiresIPhoneOS</key>
	<true/>
	<key>NSAppleMusicUsageDescription</key>
	<string>NSAppleMusicUsageDescription</string>
	<key>NSCameraUsageDescription</key>
	<string>NSCameraUsageDescription</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>NSPhotoLibraryUsageDescription</string>
	<key>NSUserNotificationsUsageDescription</key>
	<string>$(PRODUCT_NAME)からの通知を受け取るには許可が必要です</string>
	<key>UIApplicationSupportsIndirectInputEvents</key>
	<true/>
	<key>UIBackgroundModes</key>
	<array>
		<string>fetch</string>
		<string>remote-notification</string>
		<string>processing</string>
	</array>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UIMainStoryboardFile</key>
	<string>Main</string>
	<key>UIStatusBarHidden</key>
	<false/>
	<key>UISupportedInterfaceOrientations</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>UISupportedInterfaceOrientations~ipad</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationPortraitUpsideDown</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>UIViewControllerBasedStatusBarAppearance</key>
	<false/>
</dict>
</plist>

Are you sending messages to your app from the Firebase Admin SDK?

I use firebase example for sending message.
My snippet is below.

I add required headers, such as "priority" on Android and "content-available" on iOS.

from firebase_admin import credentials, initialize_app, messaging

# cred = credentials.RefreshToken("credentials.json")
default_app = initialize_app(credential=credentials.Certificate("credentials.json"))

# This registration token comes from the client FCM SDKs.
registration_token = "<device_token>"

# See documentation on defining a message payload.
message = messaging.Message(
    notification=messaging.Notification(title="TEST TITLE", body="TEST BODY"),
    data={
        "score": "850",
        "time": "2:45",
    },
    android=messaging.AndroidConfig(priority="high"),
    apns=messaging.APNSConfig(
        payload=messaging.APNSPayload(aps=messaging.Aps(content_available=True)),
        headers={"apns-priority": "5"},
    ),
    token=registration_token,
)

# Send a message to the device corresponding to the provided
# registration token.
response = messaging.send(message)
# Response is a message ID string.
print("Successfully sent message:", response)

Have you requested permission from the user to receive notifications?

  • I have the relevant permission to receive notifications.

Have you used the 'Console' application on your macOS device to check if the iOS device's system is throttling your background messages?

デフォルト 04:32:50.662028+0900 SpringBoard Received incoming message on topic com.example at priority 10
デフォルト 04:32:50.673029+0900 SpringBoard [com.example] Received remote notification request 7E65-E90C [ waking: 0, hasAlertContent: 1, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Alert]
デフォルト 04:32:50.673408+0900 SpringBoard [com.example] Process delivery of push notification 7E65-E90C
デフォルト 04:32:50.673753+0900 SpringBoard [com.example] Request DUET delivers content-available push notification to application
デフォルト 04:32:50.674520+0900 SpringBoard SUBMITTING: com.apple.pushLaunch.com.example:39932D
デフォルト 04:32:50.675441+0900 SpringBoard [com.example] Badge can be set for notification 7E65-E90C: 0 [ canBadge: 1 badgeNumber: (null) ]
デフォルト 04:32:50.675678+0900 SpringBoard Getting effectiveSectionInfo for section identifier: com.example
デフォルト 04:32:50.675880+0900 SpringBoard [com.example] Getting effective section info
デフォルト 04:32:50.677206+0900 dasd Submitted Activity: com.apple.pushLaunch.com.example:39932D at priority 10 (Sun Nov 3 04:32:50 2024 - Mon Nov 4 04:32:50 2024)
デフォルト 04:32:50.677639+0900 SpringBoard [com.example] Got effective section info [ hasResult: 1 ]
デフォルト 04:32:50.677850+0900 SpringBoard Getting effectiveSectionInfo for section identifier: com.example
デフォルト 04:32:50.678046+0900 SpringBoard [com.example] Getting effective section info
デフォルト 04:32:50.679086+0900 SpringBoard [com.example] Got effective section info [ hasResult: 1 ]
デフォルト 04:32:50.679431+0900 SpringBoard [com.example] Saving notification 7E65-E90C: YES [ hasAlertContent: YES, shouldPresentAlert: YES settingsShouldSave: YES]
デフォルト 04:32:50.680068+0900 dasd Daemon Canceling Activities: {(
com.apple.pushLaunch.com.example:731C6D
)}
デフォルト 04:32:50.680233+0900 dasd CANCELED: com.apple.pushLaunch.com.example:731C6D at priority 10 !
デフォルト 04:32:50.718648+0900 SpringBoard [com.example] Delivered user visible push notification 7E65-E90C
デフォルト 04:32:50.727308+0900 SpringBoard [com.example] Load 0 pending notification dictionaries
デフォルト 04:32:50.727812+0900 SpringBoard [com.example] Adding notification 7E65-E90C [ hasAlertContent: 1, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]; interruption-level: 1; destinations 398: (
NotificationCenter,
LockScreen,
Alert,
Spoken,
Forwarding
)
デフォルト 04:32:50.728473+0900 SpringBoard BBDataProviderProxy com.example has enqueued a bulletin request
デフォルト 04:32:50.728648+0900 SpringBoard BBDataProviderProxy com.example is now sending enqueued bulletin request to BBServer
デフォルト 04:32:50.729482+0900 SpringBoard Publishing bulletin for section <com.example>: subsectionIDs: (null), bulletinID = <64AD7851-AF33-4FE4-946C-B3E3ACACFAE3>, expiration date = <Sun Nov 10 04:32:50 2024>, expiration events <0>
デフォルト 04:32:50.734751+0900 SpringBoard NCBulletinNotificationSource adding bulletin 7E65-E90C for feed 3115 in section com.example
デフォルト 04:32:50.743469+0900 SpringBoard [com.apple.springboard.NCBulletinNotificationSource:0BB2CF28-AE1D-4DAE-8C8B-F6A5752A93DD] Resolving behavior for event, details=<DNDMutableClientEventDetails: 0x303cb9720; identifier: 'E0346579-3961-40F5-ADC6-B54BD8CA6576'; bundleIdentifier:: com.example; type: Default; urgency: Default; sender: (null); threadIdentifier: E3BBBB1022DC9959; filterCritera: (null); notifyAnyway: 0; behavior: Default>
デフォルト 04:32:50.744395+0900 donotdisturbd Event was resolved: resolution=<DNDSEventBehaviorResolution: 0x66492ce40; UUID: D9574973-7427-4816-95FD-7D2EA22124DA; date: 2024-11-02 19:32:50 +0000; eventBehavior: <DNDClientEventBehavior: 0x6642d7e70; eventDetails: <DNDClientEventDetails: 0x6648f4dc0; identifier: 'E0346579-3961-40F5-ADC6-B54BD8CA6576'; bundleIdentifier:: com.example; type: Default; urgency: Default; sender: (null); threadIdentifier: E3BBBB1022DC9959; filterCritera: (null); notifyAnyway: 0; behavior: Default>; interruptionSuppression: none; resolutionReason: disabled; activeModeUUID: (null)>; clientIdentifier: 'com.apple.springboard.NCBulletinNotificationSource'; outcome: allowed; reason: disabled>
デフォルト 04:32:50.745058+0900 SpringBoard [com.apple.springboard.NCBulletinNotificationSource:0BB2CF28-AE1D-4DAE-8C8B-F6A5752A93DD] Resolved event, details=<DNDMutableClientEventDetails: 0x303cb9720; identifier: 'E0346579-3961-40F5-ADC6-B54BD8CA6576'; bundleIdentifier:: com.example; type: Default; urgency: Default; sender: (null); threadIdentifier: E3BBBB1022DC9959; filterCritera: (null); notifyAnyway: 0; behavior: Default> behavior=<DNDClientEventBehavior: 0x30119b6c0; eventDetails: <DNDClientEventDetails: 0x303fffb60; identifier: 'E0346579-3961-40F5-ADC6-B54BD8CA6576'; bundleIdentifier:: com.example; type: Default; urgency: Default; sender: (null); threadIdentifier: E3BBBB1022DC9959; filterCritera: (null); notifyAnyway: 0; behavior: Default>; interruptionSuppression: none; resolutionReason: disabled; activeModeUUID: (null)>
デフォルト 04:32:50.745439+0900 SpringBoard Posting notification id: 7E65-E90C; section: com.example; thread: E2B6-531F; category: ; timestamp: 2024-11-02 19:32:50 +0000; interruption-level: active; relevance-score: 0.00; filter-criteria: (null); actions: [ minimal: 0 (0 text), default: 0 (0 text) ]; destinations: [ {(
BulletinDestinationCoverSheet,
BulletinDestinationBanner,
BulletinDestinationNotificationCenter,
BulletinDestinationLockScreen
)} ]
デフォルト 04:32:50.747673+0900 SpringBoard Incoming Section [NCNotificationStructuredSectionList] inserting notification request 7E65-E90C in existing group Group List [com.example:E2B6-531F] at index 0
デフォルト 04:32:50.747879+0900 SpringBoard Group List [com.example:E2B6-531F] inserting notification request 7E65-E90C at index 0

Additional context and comments

I referenced the Issue 13525, and I specified pubspec.yaml below. I used the version in which the issue is reported to be resolved. The notifications themselves are being received in any state, so the settings for FCM should be correct.

  firebase_core: "^3.6.0"
  firebase_messaging:
    git:
      url: https://github.com/firebase/flutterfire
      path: packages/firebase_messaging/firebase_messaging
      ref: 8ff0f88c512a0dde16f5906c36259b911e0d5de7
firebase_messaging:
  dependency: "direct main"
  description:
    path: "packages/firebase_messaging/firebase_messaging"
    ref: "8ff0f88c512a0dde16f5906c36259b911e0d5de7"
    resolved-ref: "8ff0f88c512a0dde16f5906c36259b911e0d5de7"
    url: "https://github.com/firebase/flutterfire"
  source: git
  version: "15.1.3"

I checked some pattern, combination build-mode(debug or profile or release) and app-state(foreground, background with app-alive, background with app-killed).
I found success message in 'Console' application on macOS 'COMPLETED com.example:5EBC1B at priority 5 !'.

In my case, under background with app-alive state and debug-mode, FirebaseMessaging.onBackgroundMessage was invoked correctly.
However, under background with app-alive and release-mode or background with app-alive and release-mode was not invoked.
As mentioned above, I found cancelled background task message 'CANCELED: com.apple.pushLaunch.com.example:731C6D at priority 5 !';
It occasionally works, but the conditions are unclear.

In background-mode and profile-build, FirebaseMessaging.onBackgroundMessage was invoked and worked correctly.

I would like to add that, all build-mode and foreground-state is work correctly.

I also tested the latest version 15.1.3 as of November 2024, as well as the currently used version 15.0.4, but they did not work properly. I tried on iOS 18 and 17, but did not observe any significant differences between the OS versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions