Ionic 1中FCMPlugin.onNotification()点击推送通知不触发求助
Hey Clara, let’s work through this FCM notification issue on iOS—this is a pretty common pitfall, so I’ve got actionable steps to help you get that onNotification() callback firing when users tap pushes.
1. Double-Check Your Push Payload Structure
iOS has strict requirements for FCM payloads to trigger post-click callbacks, especially when the app is in the background or terminated. Make sure your payload includes:
- A
datafield with custom content (this ensures the plugin can parse notification details even when the app isn’t active) - For visible notifications, add
mutable-content: 1in theapnssection to enable interaction handling - Example valid payload:
{ "notification": { "title": "Your Title", "body": "Your Message" }, "data": { "customKey": "customValue", "click_action": "FCM_PLUGIN_ACTIVITY" }, "apns": { "payload": { "aps": { "mutable-content": 1 } } }, "to": "DEVICE_TOKEN" }
2. Verify iOS Project Configuration
First, make sure your Xcode project is set up correctly for push notifications:
- Go to your project’s Capabilities tab and enable Push Notifications
- Under Background Modes, check Remote notifications (this allows the app to process push data in the background)
- Confirm your
Info.plistincludes theUIBackgroundModesarray withremote-notificationas an entry - Ensure users have granted full notification permissions (including alert/badge/sound and access to Notification Center). You can verify this in your app’s settings or add code to check authorization:
import UserNotifications UNUserNotificationCenter.current().getNotificationSettings { settings in print("Current notification settings: \(settings.authorizationStatus)") print("Alert enabled: \(settings.alertSetting)") }
3. Ensure onNotification() Is Registered Early
The callback needs to be registered immediately after the deviceready event fires—delaying this (like waiting for page load) will cause it to miss the notification tap event when the app starts from a terminated state. Here’s the correct setup:
document.addEventListener('deviceready', function() { // Register the notification listener FIRST FCMPlugin.onNotification(function(data) { console.log("Notification data:", data); if (data.wasTapped) { // App was in background/terminated and tapped to open console.log("Notification was tapped—handle action here!"); // Add your custom logic (e.g., navigate to a specific screen) } else { // App was in foreground when notification arrived } }, function(successMsg) { console.log("Notification listener registered:", successMsg); }, function(err) { console.error("Failed to register listener:", err); }); // Then register your device with FCM FCMPlugin.registerDevice(function(token) { console.log("Device FCM token:", token); }); }, false);
4. Handle Cold-Start Scenarios
When the iOS app is fully terminated (not running in the background), tapping a push notification will launch the app directly. In some cases, onNotification() won’t trigger automatically—you can fetch the initial notification data explicitly with getInitialNotification():
document.addEventListener('deviceready', function() { // Existing onNotification registration... // Check for initial notification on cold start FCMPlugin.getInitialNotification(function(data) { if (data) { console.log("Cold start notification data:", data); // Reuse your tap-handling logic here (same as data.wasTapped) } }); }, false);
5. Update Your FCM Plugin
Older versions of Cordova FCM plugins often have iOS-specific bugs with notification taps. Make sure you’re using an actively maintained plugin (like cordova-plugin-fcm-with-dependency-updated) and update to the latest version:
# Remove the old plugin cordova plugin remove cordova-plugin-fcm-with-dependency-updated # Install the latest version cordova plugin add cordova-plugin-fcm-with-dependency-updated@latest
6. Debug with Xcode Logs
Open your app in Xcode and watch the console when you tap a notification. Look for:
- FCM plugin initialization logs (confirm it’s loading correctly)
- Any error messages about parsing push payloads
- Whether the notification data is being received by the plugin
If you still run into issues, share the Xcode console output and your payload structure—this will help narrow down the problem further!
内容的提问来源于stack exchange,提问作者Clara

