Giter VIP home page Giter VIP logo

Comments (4)

eussam avatar eussam commented on August 12, 2024

Sorry I should have added some more info:

I followed your instructions in PushNotificationStarted.txt to start the service.

And my app is a Xamarin Form app, here is a code extraction for the initialization (onCreate):

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
public static Context AppContext;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        AppContext = this.ApplicationContext;

        global::Xamarin.Forms.Forms.Init (this, bundle);

        //Sender Id pour com.vetup.imrvot => 845213152730
        CrossPushNotification.Initialize<CrossPushNotificationListener>("845213152730");

        StartPushService();
        LoadApplication (new App ());
    }

from xamarin-plugins.

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:

    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.

eussam avatar eussam commented on August 12, 2024

Thanks a lot for your help, it worked,
You did explained this in your settup guide but I didn't understand it was also appropriate in the contexte of a Xamarin Form app.

Besides, I had to add the Application attribute to make it work:

    [Application]
public class VOT : Android.App.Application
{
    public static Context AppContext;

    public VOT (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>("845213152730");

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

from xamarin-plugins.

himasankar avatar himasankar commented on August 12, 2024

@eussam

Please help me to solve the same case in my application. I am also following same case that you have done in your project for push notifications functionality.

I was getting push notifications when the app is running. But when the app was closed then app not able to getting push notifications.

I have created the Android Application class that you have followed in your scenario, but I was getting below error.

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Error executing task GenerateJavaStubs: There can be only one type with an [Application] attribute; found: , (MyApplicationName.Droid).

Please suggest me, how can I solve this problem in my application. And how can I get the push notifications even app is closed.

If you have any sample code for push notifications functionality in Xamarin.Forms, Please share with me.

Thanks, Hima

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.