我在这行中收到一条错误消息:
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
错误CS0103:名称“ GoogleApiAvailability”在当前上下文中不存在
怎么了?我遵循了本教程,但仍然收到错误消息。 https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows
Activity1.cs:
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using Android.Content;
using Android.Gms.Common;
using Firebase.Messaging;
using Firebase.Iid;
using Android.Util;
namespace ggdgdgd.Android
{
[Activity(Label = "ggdgdgd.Android"
, MainLauncher = true
, Icon = "@drawable/ic_launcher"
, Theme = "@style/Theme.Splash"
, AlwaysRetainTaskState = true
, LaunchMode = LaunchMode.SingleInstance
, ScreenOrientation = ScreenOrientation.FullUser
, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
static readonly string TAG = "MainActivity";
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
string Texttest = "";
public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.Success)
{
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
Texttest = GoogleApiAvailability.Instance.GetErrorString(resultCode);
else
{
Texttest = "This device is not supported";
Finish();
}
return false;
}
else
{
Texttest = "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(Context.NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
Xamarin.Forms.Forms.Init(this, bundle);
Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
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);
}
}
var g = new Game1();
SetContentView((View)g.Services.GetService(typeof(View)));
IsPlayServicesAvailable();
CreateNotificationChannel();
g.Run();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
}
}
答案 0 :(得分:0)
您确定您拥有最新的NuGet和SDK吗?
GoogleApiAvailability
是Android.Gms.Common
命名空间的一部分,通常随您的SDK一起提供,要么您没有安装正确的Xamarin / Android SDK,要么您的版本非常旧。
也可能是一些编译器错误,可以通过重新启动来解决