Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed android notification on close application #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vurf
Copy link

@vurf vurf commented Sep 9, 2016

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

@vurf
Copy link
Author

vurf commented Sep 9, 2016

it's critical bug for android notification!!

@vurf
Copy link
Author

vurf commented Sep 9, 2016

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

@vurf
Copy link
Author

vurf commented Sep 9, 2016

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
Copy link
Owner

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
Copy link

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
Copy link

dashika commented Oct 17, 2016

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:
1. "PushNotification.Plugin.Android" - in this project find place where you must replace your package name. 2. In the class "PushNotificationsReceiver" I delete comments. 3. 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
Copy link

Acilec commented Feb 16, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants