rdelrosario / xamarin-plugins

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

When app is closed. not working remote push notication #19

Closed aybarsyalcin closed 8 years ago

aybarsyalcin commented 8 years ago

Hi, Thanks your code. During app state is open, or home button background mode, it is work. But App state is closed or kill not working push notification. Why? Please help me?

Thanks.

rdelrosario commented 8 years ago

You need to do this if you wish to run it even when app closed. Instead of initializing the plugin in the MainActivity.cs. Do the following:

Implement an Android Application class in your Droid project and initialize plugin there. Here a brief snippet:

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

Just move your initialization stuff from MainActivity.cs to this Android Application class. Also Replace YourAndroidApplication name to your App name.

Hope this helps. Let me know if need any additional help.

aybarsyalcin commented 8 years ago

Hi, I tried your code. I changed "MyAndroidApplication" name extends Application class. But I have to adding 1 line code for running application class. "[Application(Debuggable = false, ManageSpaceActivity = typeof(MainActivity))]" So, I can not receive register token function. Is that possible sample working your code. Please share me.

[Activity (Label = "MyAPPName")]
[Application(Debuggable = false, ManageSpaceActivity = typeof(MainActivity))]

public class MyAndroidApplication : Application
{
    public static Context AppContext;

    public MyAndroidApplication(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>("123123123123");

        //This service will keep your app receiving push even when closed.              
        StartPushService();
    }

    public static void StartPushService()
    {
        AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));
        if (Build.VERSION.SdkInt >= 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 (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) 
        {
            PendingIntent pintent = PendingIntent.GetService (AppContext, 0, new Intent (AppContext, typeof(PushNotificationService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService (Context.AlarmService);
            alarm.Cancel (pintent);
        }
    }

}
aybarsyalcin commented 8 years ago

Hi again, I have one question. Your code implemented my project. I watch debug on adb. when app is closed, take it "W/GCM-DMM ( 2267): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.packagename (has extras) }"

Please help why I am getting it. Thanks

homerokzam commented 8 years ago

I'm sorry, but I did not understand. When I have a Xamarin.Forms application on the Droid project already exists MainActivity class that inherits from FormsApplicationActivity. I have to create another class?

Does anyone have an example to Android using Xamarin.Forms?

Thanks, Homero

NinjaDev06 commented 8 years ago

Hi, I tried your sample and the fixed propose by aybarsyalcin and I still received notification only when the application is running. Any other solution?

@aybarsyalcin You must keep the following line in your MainActivity : CrossPushNotification.Current.Register();

jmeadecvlt commented 8 years ago

@rdelrosario, following your suggestions appears to only work some of the time. I'm getting the same android exceptions as everyone else about 75% of the time when I close my app: 10-26 21:28:09.175 I/GCM ( 1520): GCM message com.company.appname 0:1445909321961938%f9b5b66af9fd7ecd 10-26 21:28:09.175 W/GCM-DMM ( 1520): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.company.appname (has extras) } 10-26 21:28:09.695 D/ConnectivityService( 1253): handleInetConditionHoldEnd: net=0, condition=100, published condition=100

any thoughts?

rdelrosario commented 8 years ago

How are you quitting the application? By doing force quit?

jmeadecvlt commented 8 years ago

Just swiping them closed. Most of the time that results in a force close, on some occasions the service is still running.

From: Rendy Del Rosario [mailto:notifications@github.com] Sent: Tuesday, October 27, 2015 11:55 AM To: rdelrosario/xamarin-plugins Cc: Jared Meade Subject: Re: [xamarin-plugins] When app is closed. not working remote push notication (#19)

How are you quitting the application? By doing force quit?

— Reply to this email directly or view it on GitHubhttps://github.com/rdelrosario/xamarin-plugins/issues/19#issuecomment-151549090.

**Legal Disclaimer****** "This communication may contain confidential and privileged material for the sole use of the intended recipient. Any unauthorized review, use or distribution by others is strictly prohibited. If you have received the message by mistake, please advise the sender by reply email and delete the message. Thank you."


jmeadecvlt commented 8 years ago

it's critical that we keep the service running if the app is "swiped" closed. Any update to this issue?

rdelrosario commented 8 years ago

The only way to keep receiving push notifications while the app is closed is using a Service running on the background. That's how other apps do it too.

jmeadecvlt commented 8 years ago

im not disagreeing, im just stating that something isn't working properly. have you been able to reproduce this?

jmeadecvlt commented 8 years ago

just to add more info, it looks like this only occurs when debugging using visual studio and closing the app during debugging.

heres a snippet of the android logs when this occurs:

11-10 09:14:59.292 I/ActivityManager( 1252): Force stopping com.company.app appid=10066 user=0: from pid 2872
11-10 09:14:59.292 I/ActivityManager( 1252):   Force stopping service ServiceRecord{a54b0920 u0 com.company.app/md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationService}
11-10 09:14:59.362 D/AndroidRuntime( 2872): Shutting down VM
rdelrosario commented 8 years ago

Did you try testing a release build? Shouldn't happen on release

rdelrosario commented 8 years ago

The problem is when closing the app during debuging is force quit, is not a normal a close of the application.