Xamarin Android应用被杀死后无法接收自定义推送通知
Hey there, I feel your pain—iOS works flawlessly across all states, Android handles pushes fine in foreground and background, but the second the app gets killed? Nada. Let’s walk through the most common fixes for this issue, since I’ve tackled this exact scenario a few times:
1. Double-Check Your AndroidManifest.xml Configuration
First things first, make sure your Firebase messaging service is properly declared in the manifest, especially for Android 12+ where android:exported is required:
<service android:name=".MyFirebaseMessagingService" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
Don’t forget the instance ID service for token refreshes too:
<service android:name=".MyFirebaseInstanceIDService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service>
Missing either of these can prevent the system from waking your app when a push arrives in killed state.
2. Validate Your Azure Notification Hub Template Payload
When your app is killed, Android’s system handles the push directly—your OnMessageReceived method won’t fire unless the payload includes the right fields. Make sure your template includes both notification (for system-level display) and data (for your custom parameters):
{ "data": { "customKey": "$(customValue)", "message": "$(messageText)" }, "notification": { "title": "$(pushTitle)", "body": "$(pushBody)" } }
If you only send data fields without notification, Android won’t show anything in the notification shade when the app is killed, and your service won’t wake up unless you set high priority (more on that next).
3. Set High Priority for Pushes
For Android 8.0+, high priority tells the system to prioritize your push and wake the app even if it’s killed. In Azure Notification Hub, make sure you set AndroidPriority to High when sending the template push. Alternatively, add this to your payload:
"android": { "priority": "high" }
Also, for Android 13+, don’t forget to request the POST_NOTIFICATIONS runtime permission—without it, even valid pushes won’t show up.
4. Guard Against Linker/ProGuard Issues
Xamarin’s linker or ProGuard can strip out Firebase or Notification Hub classes needed for background processing. Add these rules to your ProGuard.cfg to keep them safe:
-keep class com.google.firebase.** { *; } -keep class com.microsoft.azure.notificationhubs.** { *; }
In your Xamarin project settings, under Android Options > Linker, set the linking behavior to "Sdk Assemblies Only" or add explicit exemptions for Firebase and Azure Notification Hub assemblies if you’re using "Link All Assemblies".
5. Bypass Device Battery Optimization
This is a big one—many Android OEMs (Xiaomi, Huawei, OPPO, etc.) have aggressive battery saving that blocks killed apps from receiving pushes. You’ll need to guide users to add your app to the battery optimization whitelist. Here’s a quick code snippet to trigger that settings page:
var intent = new Intent(); var packageName = PackageName; var pm = (PowerManager)GetSystemService(PowerService); if (!pm.IsIgnoringBatteryOptimizations(packageName)) { intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations); intent.SetData(Android.Net.Uri.Parse($"package:{packageName}")); StartActivity(intent); }
Start with these steps—most of the time, the issue boils down to a missing manifest entry, bad payload format, or battery optimization blocking the push. If you’re still stuck, try sending a test push directly from Firebase Console to rule out Azure Notification Hub as the culprit.
内容的提问来源于stack exchange,提问作者Steve Hawkins - Xamarin Dev

