【问题标题】:Which proguard configuration is missing for firebase messaging to work on Xamarin.Android?Firebase 消息传递在 Xamarin.Android 上工作缺少哪个 proguard 配置?
【发布时间】:2021-02-20 08:19:24
【问题描述】:

我已经在 Play 商店中部署了一个为 Android 打包的 Xamarin 表单应用程序。我尝试使用“仅 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);
    }

}

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

我没有更多错误/警告,我还尝试为程序集设置忽略链接:Xamarin.Firebase.Common;Xamarin.Firebase.Iid;Xamarin.Firebase.Messaging,但这没有帮助。

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

【问题讨论】:

    标签: c# firebase xamarin.android firebase-cloud-messaging proguard


    【解决方案1】:

    链接器有时会删除您想要保留的代码。

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

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

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

    【讨论】:

    • 我明白了,感谢您的建议和链接!仅通过将所有成员保留在类级别 (AllMembers = true) 上,我才能使其在本地使用 SDK 程序集在发布模式下工作。您是否知道设置“忽略程序集的链接” - Xamarin.Firebase.Common;Xamarin.Firebase.Iid;Xamarin.Firebase.Messaging 正如我提到的那样有意义?还是可以忽略?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2021-08-04
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多