应用被杀死时,如何通过FCM触发回调函数(含Ionic4场景)
Hey there! Let's break down how to get your FCM callback working when your Ionic 4 app is killed or closed—this is a super common gotcha with FCM, so let's fix it step by step.
First: Understand FCM's Two Message Types
FCM has two core message types, and this is the root of your issue:
- Notification Messages: Handled automatically by FCM. When your app is in the foreground, it triggers
onMessageReceived, but when the app is in the background or killed, the system shows the notification directly—your callback won't fire. - Data Messages: Fully controlled by your app. These trigger
onMessageReceivedno matter if your app is foreground, background, or killed—exactly what you need!
Why Your Current Payload Isn't Working
You're sending a mixed payload with both notification and data fields. When your app is killed, FCM treats this as a Notification Message—the system displays the notification, and your onMessageReceived callback never runs. That's why you aren't seeing the trigger you want.
Fix: Send Only Data Messages
To get the callback firing when the app is killed, remove the notification field entirely and put all your content in the data section. Then you'll handle displaying the notification and running your logic in the app itself.
Corrected Payload Example
var payload = { data: { title: title, body: body, questionnaire_id: id, appointmentID: appointmentID, is_questionnaire: 'true', notification_foreground: 'true', }, };
Ionic 4 Setup Steps
Ensure Your FCM Plugin is Configured Properly
For Capacitor projects, use@capacitor-firebase/messaging(the most up-to-date option). For Cordova, stick withcordova-plugin-fcm-with-dependency-updated—both support background callbacks.Register Background Message Listener
Capacitor:
First, enable background notifications incapacitor.config.ts:export default defineConfig({ plugins: { FirebaseMessaging: { presentationOptions: ["badge", "sound", "alert"], }, }, });Then register the listener in your app code:
import { FirebaseMessaging } from '@capacitor-firebase/messaging'; // This runs when the app is killed/backgrounded FirebaseMessaging.addListener('backgroundMessage', async (message) => { console.log('Background message received:', message); // Add your logic here—handle questionnaire ID, appointment ID, etc. });Cordova:
Make sure yourconfig.xmlhas the correct permissions, then register the background callback:FCMPlugin.onBackgroundMessage(function(payload){ console.log("Background message received", payload); // Your business logic goes here });
Manually Display Local Notifications
Since we're sending only Data Messages, the system won't show a notification automatically. You'll need to create one yourself in the callback:import { LocalNotifications } from '@capacitor/local-notifications'; // Inside your backgroundMessage listener await LocalNotifications.schedule({ notifications: [ { title: message.data.title, body: message.data.body, id: 1, sound: 'default', extra: { questionnaire_id: message.data.questionnaire_id, appointmentID: message.data.appointmentID } } ] });
Quick Extra Tips
- Permissions: Make sure users have granted notification permissions (Android needs
POST_NOTIFICATIONS, iOS requires enabling notifications in settings). - Android Doze Mode: If the device is in Doze, FCM messages might delay—use high-priority messages sparingly to avoid draining battery.
- iOS Limits: iOS has strict rules for background messages—keep payloads small and avoid heavy processing to ensure your callback runs.
内容的提问来源于stack exchange,提问作者Vijaya Mounika Velidi

