rdelrosario / xamarin-plugins

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

FATAL EXCEPTION: IntentService[null] #10

Closed eussam closed 9 years ago

eussam commented 9 years ago

Hi,

I'm having troubles to receive notifications when the app is not started, been trying to find a solution for a while w/o success...

Here is the log I got from Android Device Monitor when I send a notification while my app is not started:

06-09 16:35:45.213: E/AndroidRuntime(7385): FATAL EXCEPTION: IntentService[null] 06-09 16:35:45.213: E/AndroidRuntime(7385): Process: com.vetup.imrvot, PID: 7385 06-09 16:35:45.213: E/AndroidRuntime(7385): md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: PushNotification.Plugin.Abstractions.PushNotificationNotInitializedException: CrossPushNotification Plugin is not initialized. Should initialize before use with CrossPushNotification Initialize method. Example: CrossPushNotification.Initialize() 06-09 16:35:45.213: E/AndroidRuntime(7385): at PushNotification.Plugin.PushNotificationImplementation.OnHandleIntent (Android.Content.Intent) <0x001d4> 06-09 16:35:45.213: E/AndroidRuntime(7385): at Android.App.IntentService.n_OnHandleIntent_Landroid_contentIntent (intptr,intptr,intptr) <0x0005b> 06-09 16:35:45.213: E/AndroidRuntime(7385): at (wrapper dynamic-method) object.b1a05100-dc64-4f9b-a5c5-8647af307690 (intptr,intptr,intptr) <0x00043> 06-09 16:35:45.213: E/AndroidRuntime(7385): at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationImplementation.n_onHandleIntent(Native Method) 06-09 16:35:45.213: E/AndroidRuntime(7385): at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationImplementation.onHandleIntent(PushNotificationImplementation.java:28) 06-09 16:35:45.213: E/AndroidRuntime(7385): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 06-09 16:35:45.213: E/AndroidRuntime(7385): at android.os.Handler.dispatchMessage(Handler.java:102) 06-09 16:35:45.213: E/AndroidRuntime(7385): at android.os.Looper.loop(Looper.java:136) 06-09 16:35:45.213: E/AndroidRuntime(7385): at android.os.HandlerThread.run(HandlerThread.java:61)

Everything is fine when the app is started (got focus or in the background).

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

Thank you very much in advance for any input

Philippe

eussam commented 9 years ago

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 ());
    }
rdelrosario commented 9 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:

    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.

eussam commented 9 years ago

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();
    }
himasankar commented 8 years ago

@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