rdelrosario / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
178 stars 161 forks source link

fixed android notification on close application #88

Open vurf opened 8 years ago

vurf commented 8 years ago

CrossPushNotification.PushNotificationListener - its static and its equal null when android application close

vurf commented 8 years ago

it's critical bug for android notification!!

vurf commented 8 years ago

I don't know appveyor and his tests, but it's critical bug and it is necessary to quickly solve

vurf commented 8 years ago

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.

rdelrosario commented 8 years ago

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);
    }

} }

kvandake commented 8 years ago

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)?

dashika commented 7 years ago

If you want that android app work when it closed - you must include "PushNotification.Plugin.Android" from this resource and create Service.

  1. For it you must add in AndroidManifest.xml:
  2. "PushNotification.Plugin.Android" - in this project find place where you must replace your package name.
  3. In the class "PushNotificationsReceiver" I delete comments.
  4. As for me work this solution : In the "PushNotification.Plugin.Android" in the class PushNotificationService I rewrote to :
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");
        }
    }
}
Acilec commented 7 years ago

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!