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

Firebase推送通知无法使用及未知应用问题,咨询测试方法

Hey there! Let's work through your Firebase push notification issues together. First, let's fix that "Unknown App" problem showing up under user segmentation, then walk through all the reliable ways to test push notifications in Firebase.

一、Fixing the "Unknown App" Issue

This usually happens when Firebase hasn't properly synced with your Android app or there's a configuration gap. Try these steps:

  • Double-check Firebase app association: Make sure you've placed the google-services.json file (downloaded from Firebase console) directly in your Android project's app/ directory, then sync your Gradle project (hit the "Sync Now" button in Android Studio—you can't skip this step!).
  • Verify your AndroidManifest.xml config:
    • Ensure you've declared the Firebase Messaging service correctly (this is non-negotiable):
      <service
          android:name=".MyFirebaseMessagingService"
          android:exported="false">
          <intent-filter>
              <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
      </service>
      
    • Confirm you have the necessary permissions added:
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- Mandatory for Android 13+ -->
      
  • Give Firebase some time to sync: Sometimes, newly created apps take 10-15 minutes for Firebase to pull in device data. Refresh the console after waiting a bit.
  • Make sure a device has run the app: Firebase can't recognize your app until at least one device has installed and launched it. Grab a physical device or a Google Play-enabled emulator, install your app, open it once, then check the console again.
二、Testing Firebase Push Notifications

There are three straightforward ways to test pushes—pick the one that fits your workflow best:

1. Test Directly via Firebase Console

This is the simplest method for quick checks:

  • Head to your Firebase project's Cloud Messaging section, click "Send your first message".
  • Fill in a test title and body for your notification.
  • Under "Target", select "Single device". To get the device's FCM token:
    • Add this code to your Android app to fetch and log the token:
      FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
          if (!task.isSuccessful) {
              Log.w("FCM_TOKEN", "Fetching token failed", task.exception)
              return@addOnCompleteListener
          }
          val token = task.result
          Log.d("FCM_TOKEN", "Your device token: $token")
          // You can also display this token in your app's UI for easy access
      }
      
    • Copy the token from Logcat (or your app's UI), paste it into the console's "FCM registration token" field, then hit "Test". Your device should get the notification right away.

2. Test with HTTP Requests (Postman/Curl)

Great for testing backend-like workflows:

  • First, grab your Firebase server key: Go to Project Settings > Cloud Messaging > Server Key and copy it.
  • Use this setup for your request:
    • URL: https://fcm.googleapis.com/fcm/send
    • Headers:
      • Authorization: key=YOUR_SERVER_KEY
      • Content-Type: application/json
    • Request body example:
      {
          "to": "YOUR_DEVICE_FCM_TOKEN",
          "notification": {
              "title": "Test Push",
              "body": "This is a test from an HTTP request!"
          }
      }
      
  • Send the request, and your device should receive the notification.

3. Test with Your Backend (If You Have One)

If you're using a custom backend to send pushes:

  • Make sure your backend uses the same server key from Firebase's Project Settings.
  • Log the API requests your backend sends to FCM to confirm they're formatted correctly (match the JSON structure from the HTTP test above).
  • Check that your backend handles errors properly—FCM will return status codes if something's wrong (like an invalid token).
Extra Tips to Avoid Headaches
  • Android 13+ Permissions: You need to request the POST_NOTIFICATIONS permission at runtime (not just declare it in the manifest). Add a prompt to ask users for notification access when your app first launches.
  • Foreground vs Background Handling: Your MyFirebaseMessagingService's onMessageReceived method handles foreground notifications, while background notifications are handled by Firebase's default system channel. Make sure your service is set up correctly for both cases.
  • Emulator Notes: Only use emulators that include Google Play Services—regular emulators can't receive FCM pushes. You can select these images when creating a new emulator in Android Studio.

内容的提问来源于stack exchange,提问作者esu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:11:00