TechBuzzPro

Set Up Firebase Cloud Messaging for iOS and Android

· 5 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can set up Firebase Cloud Messaging (FCM) for both iOS and Android apps to send push notifications. This guide walks you through the exact steps to integrate FCM into your mobile apps, covering project setup, platform-specific configuration, and testing. You’ll need a Firebase account, access to the Firebase Console, and your app’s platform-specific credentials (like an iOS Apple Developer account and Android package name).

What You'll Need

  • A Google account to access Firebase Console
  • Firebase Console access at console.firebase.google.com
  • iOS: Apple Developer Program account for APNs (Apple Push Notification service) certificates
  • Android: Android Studio and a valid Android package name (e.g., com.yourcompany.yourapp)
  • iOS: Xcode and a registered iOS app ID with push notifications enabled
  • Basic knowledge of mobile app development in Swift/Kotlin or Flutter/React Native

Step-by-Step

  1. Go to Firebase Console and sign in. Click "Add project" and give it a name. Disable Google Analytics unless you plan to use it, then click "Create project".
  2. Once the project is created, click on the Android icon to add an Android app. Enter your Android package name exactly as it appears in your app’s build.gradle file. Add your app’s SHA-1 fingerprint if using authentication (you can find it via Gradle > Tasks > android > signingReport in Android Studio). Register the app.
  3. Download the google-services.json file and place it in your Android app’s app/ directory. This file links your app to Firebase.
  4. In your Android project, add the Google Services plugin. In the project-level build.gradle, add classpath 'com.google.gms:google-services:4.3.15'. In the app-level build.gradle, apply the plugin with apply plugin: 'com.google.gms.google-services'. Then add the FCM dependency: implementation 'com.google.firebase:firebase-messaging'.
  5. Sync your Gradle files. Rebuild the project to ensure there are no errors.
  6. For iOS, go back to Firebase Console, click the iOS button, and enter your iOS bundle ID (e.g., com.yourcompany.yourapp). Download the GoogleService-Info.plist file and add it to your Xcode project. Make sure it’s included in the app target.
  7. In Xcode, go to your app’s target settings, enable "Push Notifications" under the "Capabilities" tab. This configures your app ID on Apple’s servers to support push.
  8. In Firebase Console, under your project settings, go to the Cloud Messaging tab. Upload your APNs authentication key or certificate for iOS. You can generate this key in the Apple Developer portal under Keys or Certificates.
  9. In your iOS app, install Firebase via CocoaPods by adding pod 'Firebase/Messaging' to your Podfile, then run pod install. Open the .xcworkspace file afterward.
  10. In your iOS app delegate, import Firebase and call FirebaseApp.configure() in application:didFinishLaunchingWithOptions:.
  11. On both platforms, request user permission for notifications. On Android, starting with API 33, you must request POST_NOTIFICATIONS permission at runtime. On iOS, use UNUserNotificationCenter to request authorization.
  12. Handle FCM token retrieval. Implement FirebaseMessaging.getInstance().getToken() on Android and messaging.getToken() on iOS to get the registration token. Send this token to your server if you plan to target specific devices.
  13. Test the setup. Use the Firebase Console’s "Cloud Messaging" panel to send a test notification. Choose your app, enter a title and body, and send it. Check your device for the notification.

Common Mistakes

  • Using the wrong package name or bundle ID when registering the app in Firebase.
  • Forgetting to add google-services.json or GoogleService-Info.plist to the correct location.
  • Not enabling push notifications in Xcode capabilities or missing APNs setup in Firebase.
  • Skipping runtime permission requests on Android 13+ or not handling iOS notification authorization.
  • Not syncing Gradle after adding dependencies, causing build failures.

Tips

  • Use Firebase’s debug logging: on Android, enable adb logcat to see FCM logs; on iOS, check Xcode console output.
  • Always test on real devices—simulators do not fully support push notifications.
  • Store FCM tokens on your server securely and refresh them when updated.
  • Use topics for broad messaging (e.g., news, updates) instead of targeting individual tokens.
  • Handle token refresh in the background—tokens can expire or change after app reinstalls.

Troubleshooting

If notifications don’t arrive, first verify the app is registered correctly in Firebase and the config file is in place. On Android, ensure Google Play Services are available—some emulators or devices lack them. On iOS, confirm the APNs key is uploaded and valid. Check that the device has internet access and is not in power-saving mode.

If you get a "token retrieval failed" error, ensure the app is connected to the internet and Firebase is initialized. On Android, check that the google-services plugin is applied correctly. On iOS, confirm FirebaseApp.configure() runs early in the app lifecycle.

If notifications appear in logs but not on device, check system notification settings—users may have disabled alerts. On Android, verify the notification channel is created and not muted. On iOS, ensure the app has permission and the payload isn’t silent.

For silent push issues (data-only messages), ensure the FCM message includes "content_available": true and "priority": "high" for iOS, and on Android, ensure the app is in the background to trigger the onMessageReceived callback.

Frequently Asked Questions

Can I use FCM without Google Play Services?

No. Android devices require Google Play Services to receive FCM messages. Devices without it, like some Huawei phones or custom ROMs, won’t work.

Do I need a paid Apple Developer account?

Yes. You need a paid Apple Developer account to generate APNs certificates or keys and register your app ID for push notifications.

Why are notifications delayed?

FCM is optimized for battery efficiency and may delay messages. For time-critical alerts, use high-priority messages and ensure the device isn’t in Doze mode (Android) or background app refresh is off (iOS).

How do I send data-only messages?

Send a message with no notification key—only data. Handle it in onMessageReceived on Android or didReceiveRemoteNotification on iOS.

Can I use FCM with Flutter or React Native?

Yes. Use official plugins like firebase_messaging for Flutter or @react-native-firebase/messaging for React Native. Setup steps are similar but adapted to the framework.

What happens if the FCM token expires?

Tokens can expire after app reinstallation or security updates. Always listen for token refresh events and update your server with the new token.