Solution 1 :
I have added the nuget package “Xamarin.Firebase.Analytics” and it has worked. This package is necessary everytime we use a project with the Google Analytics set up during creation.
Problem :
I am trying for the first time to implement Firebase Cloud Messaging in my Xamarin Forms Android application.
When I run the application, the FirebaseApp initialization is successful, the parsing of json file is ok and the token is generated.
The problem is the registration of the app in the Firebase webpage is not happening. The last view of the registration is the following:
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. I added json file to project and change its Build Action to 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. 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.
See the code:
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>
Anybody have any idea to fix this?
I am using:
Visual Studio 2019
Xamarin.Forms v4.7.0.1080
Xamarin.GooglePlayServices.Base v71.1610.1
Xamarin.Firebase.Messaging v71.1740.1