在 Firebase 控制台项目中添加 Xamarin.Android 应用程序时出现问题
Issue adding Xamarin.Android app in Firebase Console Project
我第一次尝试在我的 Xamarin Forms Android 应用程序中实施 Firebase 云消息传递。
当我运行应用程序时,FirebaseApp初始化成功,解析json文件并生成token。
问题是应用程序在 Firebase 网页中的注册没有发生。注册的最后一个视图如下:
而且它永远不会停止验证应用程序是否已连接到他们的服务器。
我仔细检查了两个项目中包的名称是否相同。我将 json 文件添加到项目并将其构建操作更改为 GoogleServicesJson。我卸载并重新安装了设备中的应用程序以再次获取令牌。我试图在浏览器中关闭并重新打开 Firebase 页面。我尝试了很多次 运行 应用程序与我的应用程序到服务器的 Firebase 网页扫描连接,但结果总是一样。
见代码:
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
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
<?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>
有人知道如何解决这个问题吗?
我正在使用:
Visual Studio 2019
Xamarin.Forms v4.7.0.1080
Xamarin.GooglePlayServices.Base v71.1610.1
Xamarin.Firebase.Messaging v71.1740.1
我添加了 nuget 包“Xamarin.Firebase.Analytics”,它已经工作了。每当我们使用一个在创建过程中设置了 Google Analytics 的项目时,这个包都是必需的。
我第一次尝试在我的 Xamarin Forms Android 应用程序中实施 Firebase 云消息传递。
当我运行应用程序时,FirebaseApp初始化成功,解析json文件并生成token。
问题是应用程序在 Firebase 网页中的注册没有发生。注册的最后一个视图如下:
而且它永远不会停止验证应用程序是否已连接到他们的服务器。
我仔细检查了两个项目中包的名称是否相同。我将 json 文件添加到项目并将其构建操作更改为 GoogleServicesJson。我卸载并重新安装了设备中的应用程序以再次获取令牌。我试图在浏览器中关闭并重新打开 Firebase 页面。我尝试了很多次 运行 应用程序与我的应用程序到服务器的 Firebase 网页扫描连接,但结果总是一样。
见代码:
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
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
<?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>
有人知道如何解决这个问题吗?
我正在使用:
Visual Studio 2019
Xamarin.Forms v4.7.0.1080
Xamarin.GooglePlayServices.Base v71.1610.1
Xamarin.Firebase.Messaging v71.1740.1
我添加了 nuget 包“Xamarin.Firebase.Analytics”,它已经工作了。每当我们使用一个在创建过程中设置了 Google Analytics 的项目时,这个包都是必需的。