You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

应用被杀死时,如何通过FCM触发回调函数(含Ionic4场景)

解决Ionic 4应用被杀死时FCM回调触发问题

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 onMessageReceived no 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

  1. Ensure Your FCM Plugin is Configured Properly
    For Capacitor projects, use @capacitor-firebase/messaging (the most up-to-date option). For Cordova, stick with cordova-plugin-fcm-with-dependency-updated—both support background callbacks.

  2. Register Background Message Listener

    • Capacitor:
      First, enable background notifications in capacitor.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 your config.xml has the correct permissions, then register the background callback:

      FCMPlugin.onBackgroundMessage(function(payload){
          console.log("Background message received", payload);
          // Your business logic goes here
      });
      
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:50