Xamarin.Forms - Android/iOS : Push notification not working if app is killed

Viewed 1427

I have used https://github.com/CrossGeeks/PushNotificationPlugin for push notifications in my xamarin.forms application.

The major issue I am facing currently is Notifications not arriving in device once the app is closed. Here is how the push notifications working:

  • it works if app is in foreground or background,
  • if kill app and then send notification it not works
  • if you again open app and it's again either in background or foreground notifications not works
So it works only first time when app is installed in device and it's in 
either Foreground or background. 

Anyone know how to handle this issue?


I have initialised plugin in both Android and iOS also setup all required things.

Version of packages I have used:

  • Xamarin.Forms - 4.7.0.968
  • Plugin.PushNotification - 1.3.0

1 Code in App.xaml.cs OnStartMethod

CrossPushNotification.Current.OnTokenRefresh += (s, p) =>
{
    if (Device.RuntimePlatform == Device.Android)
    {
        DeviceToken = p.Token;
    }
};

CrossPushNotification.Current.OnNotificationReceived += async (s, p) =>
{
    //Logic to handle arrived notification 
};

CrossPushNotification.Current.OnNotificationOpened += async (s, p) =>
{
    //Notification tap handler code here
};

CrossPushNotification.Current.OnNotificationDeleted += (s, p) =>
{
    Debug.WriteLine("Dismissed");
}; 

2 Android Main Application class

[Application]
public class MainApplication : Application
{
    public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();

        //Set the default notification channel for your app when running Android Oreo
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            //Change for your default notification channel id here
            PushNotificationManager.DefaultNotificationChannelId = "DefaultChannel";

            //Change for your default notification channel name here
            PushNotificationManager.DefaultNotificationChannelName = "General";
        }

        //If debug you should reset the token each time.
#if DEBUG
        PushNotificationManager.Initialize(this, true);
#else

        PushNotificationManager.Initialize(this, false);
#endif

        //PushNotificationManager.IconResource = Resource.Drawable.icon;

        //Handle notification when app is closed here
        CrossPushNotification.Current.OnNotificationReceived += (s, p) =>
        {
                
        };
    }
}

3 Android: MainActivity class

protected async override void OnCreate(Bundle savedInstanceState)        
{
    LoadApplication(new App());
    PushNotificationManager.ProcessIntent(this, Intent);
}
0 Answers
Related