Giter VIP home page Giter VIP logo

Comments (14)

rdelrosario avatar rdelrosario commented on August 12, 2024

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.

from xamarin-plugins.

aybarsyalcin avatar aybarsyalcin commented on August 12, 2024

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

}

from xamarin-plugins.

aybarsyalcin avatar aybarsyalcin commented on August 12, 2024

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

from xamarin-plugins.

homerokzam avatar homerokzam commented on August 12, 2024

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

from xamarin-plugins.

NinjaDev06 avatar NinjaDev06 commented on August 12, 2024

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

from xamarin-plugins.

jmeadecvlt avatar jmeadecvlt commented on August 12, 2024

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

from xamarin-plugins.

rdelrosario avatar rdelrosario commented on August 12, 2024

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

from xamarin-plugins.

jmeadecvlt avatar jmeadecvlt commented on August 12, 2024

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:[email protected]]
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//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."


from xamarin-plugins.

jmeadecvlt avatar jmeadecvlt commented on August 12, 2024

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

from xamarin-plugins.

rdelrosario avatar rdelrosario commented on August 12, 2024

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.

from xamarin-plugins.

jmeadecvlt avatar jmeadecvlt commented on August 12, 2024

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

from xamarin-plugins.

jmeadecvlt avatar jmeadecvlt commented on August 12, 2024

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

from xamarin-plugins.

rdelrosario avatar rdelrosario commented on August 12, 2024

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

from xamarin-plugins.

rdelrosario avatar rdelrosario commented on August 12, 2024

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

from xamarin-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.