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:
If you need any more information, please do let me know! Thanks in advance!
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")))
{
}
}