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:
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
applicationIdexactly matches the one in your app'sbuild.gradle(it's case-sensitive!).
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 like23.0.0forfirebase-messagingandfirebase-coreto maintain compatibility).
- Your Azure Notification Hubs SDK (
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.jsonis in theapp/folder).
- 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
Issue 2: onMessageReceived not triggering in your FirebaseMessagingService subclass
This is often due to message type, manifest misconfiguration, or dependency conflicts. Try these fixes:
Check Manifest Registration
- Ensure your custom
FirebaseMessagingServiceis correctly registered inAndroidManifest.xmlwith 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.** { *; }
- Ensure your custom
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—
onMessageReceivedwon't fire. It only triggers if the app is in the foreground. - Data Messages: These always trigger
onMessageReceived, regardless of app state.
- Notification Messages: When your app is in the background, the system handles displaying the notification directly—
- For testing, use Azure's Custom Payload option to send a data message like this:
{ "data": { "title": "Test Push", "body": "This should trigger onMessageReceived" } }
- FCM has two message types that behave differently:
Remove Conflicting Dependencies
- Your
build.gradleincludes bothplay-services-gcm(old GCM library) andfirebase-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.
- Your
Confirm Firebase Initialization
- Make sure Firebase is initialized properly in your app. If you don't have a custom
Applicationclass, add this line to your main activity'sonCreatemethod:FirebaseApp.initializeApp(this); - Double-check that
google-services.jsonmatches your Firebase project and is placed correctly in theapp/directory.
- Make sure Firebase is initialized properly in your app. If you don't have a custom
内容的提问来源于stack exchange,提问作者Stefan Kairinos
相关产品推荐
相关产品推荐

