要在 Xamarin.Android 上运行 firebase 消息传递,缺少哪个混淆器配置?

Which proguard configuration is missing for firebase messaging to work on Xamarin.Android?

我有一个为 Android 打包的 Xamarin 表单应用程序,我已经在 Play 商店中部署了它。我尝试使用“仅 SDK 程序集”进行链接以及以下 proguard.cfg 文件:

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**
-keep class com.google.gms.** { *; }
-dontwarn com.google.gms.**
-keep class com.google.firebase.** { *; }
-dontwarn com.google.firebase.**
-keep class android.support.v7.widget.** { *; }
-dontwarn android.support.v7.widget.*
-keep class android.support.v4.widget.Space { *; }
-dontwarn android.support.v4.widget.Space

我注意到该应用程序总体上运行良好,但 Firebase 从未使用此配置将令牌返回给该应用程序。我正在使用 FirebaseMessagingService 的以下实现,在我的例子中 OnNewToken(string) 从未被调用过:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    public MyFirebaseMessagingService()
    {

    }
    public override async void OnNewToken(string token)
    {
        try
        {
            await SecureStorage.SetAsync("fcm_token", token);
        }
        catch (Exception e)
        {
            // Possible that device doesn't support secure storage on device.
        }
    }
    public override void OnMessageReceived(RemoteMessage p0)
    {
        base.OnMessageReceived(p0);
        new NotificationHelper().CreateNotification(p0.Data);
    }

}

该应用程序在调试模式下完美运行,并且在链接选项设置为“None”的情况下发布 - 这是我目前在 Play 商店中拥有的应用程序,因此该应用程序至少可以运行......但显然我想让它更干净,我很确定我离它不远了。

我没有更多 errors/warnings 并且我还尝试为程序集设置忽略链接:Xamarin.Firebase.Common;Xamarin.Firebase.Iid;Xamarin.Firebase.Messaging 但这没有帮助。

为了使 firebase 消息传递与链接设置为“仅 SDK 程序集”一起工作,我在这里缺少什么?

linker 有时会删除您想保留的代码。

您可以使用 Android.Runtime.Preserve 属性。

public class Example
{
    [Android.Runtime.Preserve]
    public Example ()
    {
    }
}

更多详情,请查看下面的link。 https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker