Connect to a real Android device with the debug version of VisualStudio 2019 Xamarin forms
Booting (first) will get Token correctly, but
After installing and booting from an open Google Play Console test on a real machine,
I am having trouble getting Token because the OnNewToken event does not occur.
The OnNewToke event did not occur even though I tried to force it to initialize.
FirebasePushNotificationManager.Initialize(this,true); /MainActivity.cs:
FirebaseIDService.cs
class FirebaseIDService
{
Service
[IntentFilter(new[]{"com.google.firebase.MESSAGING_EVENT")]
public class FirebaseInstanceIDService:FirebaseMessagingService
{
public override void OnNewToken (string token)
{
base.OnNewToken(token);
// token write to server.
}
:
MainActivity.cs
using Plugin.FirebasePushNotification;
:
public class MainActivity: global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
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());
// Set the default notification channel for your app when running Android Oreo
if(Build.VERSION.SdkInt>=Android.OS.BuildVersionCodes.O)O)
{
// Change for your default notification channel id here
FirebasePushNotificationManager.DefaultNotificationChannelId="FirebasePushNotificationChannel";
// Change for your default notification channel name here
FirebasePushNotificationManager.DefaultNotificationChannelName="General";
}
# if DEBUG
FirebasePushNotificationManager.Initialize(this,true);
# else
FirebasePushNotificationManager.Initialize(this, false);
#endif
// Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived+=(s,p)=>
{
// Console.WriteLine("Handle notification when app is closed here";
};
}
AndroidManifest.xml
<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.int.RECEIVE"/>
<action android:name="com.google.android.c2dm.int.REGISTRATION"/>
<category android:name="${applicationId}"/>
</intent-filter>
</receiver>
<meta-data android:name="com.google.firebase.message.default_notification_icon"android:resource="@drawable/ic_stat_ic_notification"/>
running environment:
Microsoft Visual Studio Professional 2019 Version 16.8.3
Xamarin.Forms 5.0.0.1874
Xamarin.Firebase.Messaging 121.0.1
Plugin.FirebasePushNotification 3.3.10
I solved myself.
OnNewToken is a version built with the designation "none" in the Android Project Properties Android Options link.
However, it's a size warning, so it's very sad.
© 2024 OneMinuteCode. All rights reserved.