Search code examples
androidxamarin.formspush-notificationvisual-studio-app-center

Xamarin.Forms AppCenter Push Notification


Currently, I am using Xamarin.Forms with AppCenter Push, in other, to have notifications. My end goal is to have this functionality:

If the app is closed OR opened I can receive a notification (listed inside notifications list) and on click, redirect the user to a certain page. Based on the custom data provided, I am going to redirect him to a different page. What I've found in AppCenter Push docs for Xamarin.Forms are that if the app is opened, my method should have the new notification event, but it will not - display the notification.

Here is my very simple code: App.xaml.cs

protected override async void OnStart()
{
    await Push.SetEnabledAsync(true);
    if (!AppCenter.Configured)
    {
        Push.PushNotificationReceived += (sender, e) =>
        {
            DependencyService.Resolve<IMessageService>().LongAlert("TESTT");
            // Add the notification message and title to the message
            string summary = $"Push notification received:" +
                                $"\n\tNotification title: {e.Title}" +
                                $"\n\tMessage: {e.Message}";

            // If there is custom data associated with the notification,
            // print the entries
            if (e.CustomData != null)
            {
                summary += "\n\tCustom data:\n";
                foreach (string key in e.CustomData.Keys)
                {
                    summary += $"\t\t{key} : {e.CustomData[key]}\n";
                }
            }

            // Send the notification summary to debug output
            Debug.WriteLine(summary);
        };
    }

    AppCenter.Start("", typeof(Push));
}

Then I have: MainActivity.cs

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    Push.CheckLaunchedFromNotification(this, intent);
}

Struggling to implement this simple scenario I have faced different problems:

  1. When I first start the app and send a notification (while the app is opened), the handler of "PushNotificationReceived" it's not hit. Then I leave the app in the background and send again a notification. Now this time I receive the notification inside my "Notifications List" on Android and when I click it, the app is opening, then it goes through "OnNewIntent" and then it's hitting "PushNotificationReceived". Every notification after that is doing the same thing (so it's working properly) no matter if the app is running on foreground or background. To summarize, the problem is that first time when I start the app, I don't receive notifications and I receive them only if I minimize it and the open a new notification, so I go through the method "OnNewIntent".
  2. The second scenario is how can I know if "PushNotificationReceived" has been triggered while the app is working in the background or foreground, based on that I want to run different logic. Because apparently, AppCenter will not show a notification if the app is opened, then I want to create my own, but in that case, I don't know was the app opened while notification came or not.

If you need any more information, please do let me know! Thanks in advance!


Solution

  • Try doing the notifications events in the android. To pass the parameters from Android to Forms App, you can use MessagingCenter. In the below, notification received event, you can extract the data from the notification payload and pass to forms app using MessagingCenter. Ensure you have done the below steps..

    In the OnCreate method of your MainActivity, register and subscribe for PushNotification.

     if (!AppCenter.Configured)
     {
        Push.PushNotificationReceived += (sender, e) =>
        {
    
        };
      }
    

    In the NewIntent method,

    protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
    
            Push.CheckLaunchedFromNotification(this, intent);
    
            var type = intent.GetStringExtra("type");
            if ((!string.IsNullOrEmpty(type) && object.Equals(type, "notification")))
            {
    
            }
        }