Open vurf opened 8 years ago
it's critical bug for android notification!!
I don't know appveyor and his tests, but it's critical bug and it is necessary to quickly solve
You are using static CrossPushNotification.PushNotificationListener. When application is closed It is equal null. because of this warning does not work in a closed application.
Actually it does work with app closed if you initialize the plugin on an Application class.
[Application] public class YourAndroidApplication : Application { public static Context AppContext;
public YourAndroidApplication(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>("<ANDROID SENDER ID>");
//This service will keep your app receiving push even when closed.
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);
}
} }
The opportunity has not always, becouse an initialization point may be in Setup.cs(MvvmCross). Can I add initialization code in Setup.cs (Setup.cs initialized in Droid.SplashScreen.cs)?
If you want that android app work when it closed - you must include "PushNotification.Plugin.Android" from this resource and create Service.
namespace` PushNotification.Plugin
{
[Service]
public class PushNotificationService : Service, IPushNotificationListener
{
public override void OnCreate()
{
base.OnCreate();
System.Diagnostics.Debug.WriteLine("Push Notification Service - Created");
}
public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
System.Diagnostics.Debug.WriteLine("Push Notification Service - Started");
return StartCommandResult.Sticky;
}
public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
{
System.Diagnostics.Debug.WriteLine("Push Notification Service - Binded");
return null;
}
public override void OnDestroy()
{
System.Diagnostics.Debug.WriteLine("Push Notification Service - Destroyed");
base.OnDestroy();
}
private void ProcessOnMessage(string message)
{
//event - came new message and we may doing as we want
}
private async void OnCancelAlert(object sender)
{
Debug.WriteLine("Cancel alert");
}
public void OnError(string message, DeviceType deviceType)
{
Logs.LogEx(message);
}
public bool ShouldShowNotification()
{
return true;
}
public void OnMessage(JObject values, DeviceType deviceType)
{
ProcessOnMessage(values.ToString());
}
public void OnRegistered(string token, DeviceType deviceType)
{
}
public void OnUnregistered(DeviceType deviceType)
{
Debug.WriteLine("Push Notification - Device Unnregistered");
}
}
}
I'm also having this problem please help! Using Xam.Plugin.PushNotification version="1.2.4"
Everything works very well until app is closed. Then I get "Object reference not set to an instance of an object" on PushNotification.Plugin.PushNotificationGcmListener.OnMessageReceived and found the same that "CrossPushNotification.PushNotificationListener" is null when executing "CrossPushNotification.PushNotificationListener.OnMessage(values, DeviceType.Android);"
I added the StartPushService() part to my Mainactivity, but it doesn't help:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity { public static Context AppContext;
protected override void OnCreate(Bundle bundle)
{
try
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
AppContext = this.ApplicationContext;
System.Diagnostics.Debug.WriteLine("MainActivity OnCreate");
//Should specify android Sender Id as parameter
CrossPushNotification.Initialize<CrossPushNotificationListener>("xxxxxxxxxxxx");
LoadApplication(new Portable.App());
//This service will keep your app receiving push even when closed.
StartPushService();
}
catch{}
}
...
What have I done wrong? Don't understand dashikas fix or how to implement rdelrosario [Application] in other ways. Please help!
CrossPushNotification.PushNotificationListener - its static and its equal null when android application close