简体   繁体   English

在 Firebase 控制台项目中添加 Xamarin.Android 应用程序的问题

[英]Issue adding Xamarin.Android app in Firebase Console Project

I am trying for the first time to implement Firebase Cloud Messaging in my Xamarin Forms Android application.我第一次尝试在我的 Xamarin Forms ZE84ZDBE30B939808CDB6 应用程序中实现 Firebase 云消息传递

When I run the application, the FirebaseApp initialization is successful, the parsing of json file is ok and the token is generated.当我运行应用程序时,FirebaseApp 初始化成功,json 文件解析成功,生成令牌。

FirebaseApp 初始化

ID

令牌

The problem is the registration of the app in the Firebase webpage is not happening.问题是没有在 Firebase 网页中注册应用程序。 The last view of the registration is the following:注册的最后一个视图如下:

Firebase 网页

And it never stops verifying if the app has connected to their servers.它永远不会停止验证应用程序是否已连接到他们的服务器。

I double-checked if the name of the package is the same in both projects.我仔细检查了两个项目中 package 的名称是否相同。 I added json file to project and change its Build Action to GoogleServicesJson.我添加了 json 文件来投影并将其构建操作更改为 GoogleServicesJson。 I uninstalled and reinstalled the app in the device to get the token again.我在设备中卸载并重新安装了该应用程序以再次获取令牌。 I tried to close and reopen the Firebase page in the browser.我试图在浏览器中关闭并重新打开 Firebase 页面。 And I tried so many times to run the application with the Firebase webpage scanning connection of my the app to the server but the result is always the same.我尝试了很多次使用我的应用程序到服务器的 Firebase 网页扫描连接来运行应用程序,但结果始终相同。

See the code:见代码:

MainActivity.cs MainActivity.cs

namespace App2.Droid
{
    [Activity(Label = "App2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        static readonly string TAG = "MainActivity";

        internal static readonly string CHANNEL_ID = "my_notification_channel";
        internal static readonly int NOTIFICATION_ID = 100;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            if (Intent.Extras != null)
            {
                foreach (var key in Intent.Extras.KeySet())
                {
                    var value = Intent.Extras.GetString(key);
                    Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                }
            }

            Log.Debug(TAG, "google app id: " + GetString(Resource.String.google_app_id));

            IsPlayServicesAvailable();

            CreateNotificationChannel();
        }

        public bool IsPlayServicesAvailable()
        {
            int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.Success)
            {
                if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
                    Log.Debug(TAG, GoogleApiAvailability.Instance.GetErrorString(resultCode));
                else
                {
                    Log.Debug(TAG, "This device is not supported");
                    Finish();
                }
                return false;
            }
            else
            {
                Log.Debug(TAG, "Google Play Services is available.");
                return true;
            }
        }

        void CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification
                // channel on older versions of Android.
                return;
            }

            var channel = new NotificationChannel(CHANNEL_ID,
                                                  "FCM Notifications",
                                                  NotificationImportance.Default)
            {

                Description = "Firebase Cloud Messages appear in this channel"
            };

            var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
            notificationManager.CreateNotificationChannel(channel);
        }
    }
}

AndroidContent.cs安卓内容.cs

namespace App2.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";

        public override void OnNewToken(string p0)
        {
            base.OnNewToken(p0);
            Log.Debug(TAG, p0);
        }

        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        }
    }
}

AndroidManifest.xml AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.app2" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="26" android:targetSdkVersion="28" />
    <application android:label="App2.Android">
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>

Anybody have any idea to fix this?有人有任何想法来解决这个问题吗?

I am using:我在用:

Visual Studio 2019视觉工作室 2019

Xamarin.Forms v4.7.0.1080 Xamarin.Forms v4.7.0.1080

Xamarin.GooglePlayServices.Base v71.1610.1 Xamarin.GooglePlayServices.Base v71.1610.1

Xamarin.Firebase.Messaging v71.1740.1 Xamarin.Firebase.Messaging v71.1740.1

I have added the nuget package "Xamarin.Firebase.Analytics" and it has worked.我已经添加了 nuget package "Xamarin.Firebase.Analytics" 并且它已经工作了。 This package is necessary everytime we use a project with the Google Analytics set up during creation.每次我们在创建期间使用带有 Google Analytics 设置的项目时,都需要这个 package。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM