1
I made several attempts to send Push Notification and none of them I was able to view in my App. I always thought until now, that I was wrong. But I followed two steps and saw that with the guys it was working and with me it wasn’t. So I started to think otherwise, that either the firebase wasn’t firing properly or my equipment, for hitherto unknown reasons, is not receiving. How did I come to this conclusion? Is that when I start the application, I break in several points and the moment I climb the app, on Onregistered the app for and there I get the token generated. Once I deregistered and registered and generated a new message and was with the previous token and not the new generated, then when I triggered the notification, gave error in firebase of :
Status of message: failed
I entered the valid token, gave the message of Envoy and in the App I can’t catch it. Something is blocking the receipt and I don’t know what it is. I’m suspicious of the Security Master, Vysor, My KNOX and etc or own Firebase, but I’m not sure. Look at the code I just made, which reported all above: Boot class
[Application]
public class PushNotificationAppStater : Application
{
public static Context AppContext;
public PushNotificationAppStater(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
AppContext = this.ApplicationContext;
//TODO: Initialize CrossPushNotification Plugin
//TODO: Replace string parameter with your Android SENDER ID
//TODO: Specify the listener class implementing IPushNotificationListener interface in the Initialize generic
CrossPushNotification.Initialize<CrossPushNotificationListener>("806431458293");
StartPushService();
}
public static void StartPushService()
{
AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
public static void StopPushService()
{
AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
}
Message and status system class
public class CrossPushNotificationListener : IPushNotificationListener
{
public void OnError(string message, DeviceType deviceType)
{
}
public void OnMessage(JObject values, DeviceType deviceType)
{
}
public void OnRegistered(string token, DeviceType deviceType)
{
}
public void OnUnregistered(DeviceType deviceType)
{
}
public bool ShouldShowNotification()
{
return true;
}
}
Mainactivity
[Activity(Label = "App1", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
My App.xaml.Cs public partial class App : Application { public App() { Initializalizecomponent();
MainPage = new App1.MainPage();
}
protected override void OnStart()
{
CrossPushNotification.Current.Unregister();
CrossPushNotification.Current.Register();
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
My android.manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.global.app.br" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" />
<application android:label="App1.Android" android:icon="@drawable/icon">
<service
android:name=".Messaging.FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
The rest has the google-services.json with build option for google-services.json and the installed package is: Xam.Plugin.Pushnotification Newtonsoft.json
These were the only plugins installed by me. What might be this problem?
Did you test receive the notification with the app in background ???
– Matheus Rodrigues
@pnet where are the receivers declared in the manifest? Where are the firebase receivers and services classes??? Remove everything from your project and follow exactly what is written in this guide of Xamarin’s documentation. I did it for him and it worked right. But here he makes the type of basic notification, then has to see to implement his own notification.
– Grupo CDS Informática
@Grupocdsinformática, I tried to do for this example, but I was giving the go when trying to install this reference: Xamarin.Google.Play.Services.Base, said it was not possible to install in a Mono project.Droid I think, I even have a post answered by LINQ. I use Xamarin.Forms, but I’ll do another project from scratch with this example. You or Colleta told me if I had installed google-services or something like that on my phone, I don’t know if it is, but I open a lot of stuff from google, like gmail, Chrome and so on. It has to do with this?
– pnet
@Grupocdsinformática, when I try to catch the token I have this error: Java.lang.Illegalstateexception: Default Firebasepp is not initialized in this process com.inet.Droid.br. Make sure to call Firebasepp.initializeApp(Context) first. occurred. It says that firebaseApp was not initialized. How do you do it? I was doing it as an example. The error is in this line: Log.Debug(TAG, "Instanceid token: " + Firebaseinstanceid.Instance.Token);
– pnet
I saw that in the properties of google-services.json in Build Action I do not have the option Googleservicesjson
– pnet
Firebasepp.initializeApp(Context) you have to do in Application (speaking of Android blz), and Googleservices.json is not compiled at all. But at least in the android Apps, it will imbutido.
– Grupo CDS Informática
Let’s move the conversation to the chat, I’ll help you solve the problem.
– Grupo CDS Informática