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

Azure通知中心问题:从令牌提供程序获取的令牌错误

Troubleshooting Azure Notification Hubs Push Issues for Android

Let's break down your two main issues and walk through actionable fixes:

Issue 1: "The Token obtained from the Token Provider is wrong" during Azure test push

This error usually stems from misconfiguration between Azure Notification Hubs and Firebase Cloud Messaging (FCM). Here's how to resolve it:

  1. Verify FCM Server Key in Azure

    • Go to your Firebase Console, navigate to your project's Cloud Messaging tab, and copy the Server Key (avoid the Legacy Server Key unless you're maintaining an older project).
    • Head to your Azure Notification Hub, open the Google (FCM) settings, paste this key, save changes, and retry the test push.
    • Double-check that your Firebase project's applicationId exactly matches the one in your app's build.gradle (it's case-sensitive!).
  2. Update Outdated SDK Versions

    • Your Azure Notification Hubs SDK (0.4) is extremely outdated—this can cause compatibility issues with newer FCM versions. Replace it with the latest stable release:
      implementation 'com.microsoft.azure:notification-hubs-android-sdk:1.1.12@aar'
      
    • Your Firebase dependencies (17.0.0) are also old. Ensure all Firebase components use the same version (e.g., upgrade to a recent stable version like 23.0.0 for firebase-messaging and firebase-core to maintain compatibility).
  3. Validate the FCM Token

    • When your app registers for notifications, log the generated FCM Token to the console. Use this exact token to send a test push directly from Azure (instead of targeting the entire Android platform). If the error persists, the token itself is invalid—confirm your app is linked to the correct Firebase project (check that google-services.json is in the app/ folder).

Issue 2: onMessageReceived not triggering in your FirebaseMessagingService subclass

This is often due to message type, manifest misconfiguration, or dependency conflicts. Try these fixes:

  1. Check Manifest Registration

    • Ensure your custom FirebaseMessagingService is correctly registered in AndroidManifest.xml with the right intent filter:
      <service
          android:name=".YourCustomMessagingService"
          android:exported="false">
          <intent-filter>
              <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
      </service>
      
    • If you're using ProGuard, add these rules to prevent obfuscation of Firebase/Azure classes:
      -keep class com.google.firebase.** { *; }
      -keep class com.microsoft.windowsazure.messaging.** { *; }
      
  2. Understand FCM Message Types

    • FCM has two message types that behave differently:
      • Notification Messages: When your app is in the background, the system handles displaying the notification directly—onMessageReceived won't fire. It only triggers if the app is in the foreground.
      • Data Messages: These always trigger onMessageReceived, regardless of app state.
    • For testing, use Azure's Custom Payload option to send a data message like this:
      {
          "data": {
              "title": "Test Push",
              "body": "This should trigger onMessageReceived"
          }
      }
      
  3. Remove Conflicting Dependencies

    • Your build.gradle includes both play-services-gcm (old GCM library) and firebase-messaging (FCM, the official replacement). These libraries conflict and can break message handling. Delete this line:
      implementation 'com.google.android.gms:play-services-gcm:15.0.1'
      
    • Sync your Gradle project after removing the dependency.
  4. Confirm Firebase Initialization

    • Make sure Firebase is initialized properly in your app. If you don't have a custom Application class, add this line to your main activity's onCreate method:
      FirebaseApp.initializeApp(this);
      
    • Double-check that google-services.json matches your Firebase project and is placed correctly in the app/ directory.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:23:23