'Tap on notification restart application and NotificationTapped event not firing
I am using Plugin.LocalNotification
to receive notification. I want to use NotificationCenter.Current.NotificationTapped event but on Android device event is not triggered. On iOS device, it work's
I created 2 views :
- MainView
- NotifView
I send notif from NotifView page, I left app (by clicking on home button). 5 seconds later I got notification, I click on it, app restart with splashscreen. Event and treaments are not executed.
MainView.xaml.cs
public MainView()
{
InitializeComponent();
NotificationCenter.Current.NotificationTapped += Current_NotificationTapped;
var vm = new MainViewModel(this);
BindingContext = vm;
_ = vm.Initialize();
}
private void Current_NotificationTapped(NotificationEventArgs e)
{
Tour tour = JsonConvert.DeserializeObject<Tour>(e.Request.ReturningData);
Device.BeginInvokeOnMainThread(() =>
{
DisplayAlert("", "it work's", "ok");
//(App.Current as App).Main.Navigation.PushModalAsync(new Settings());
});
}
NotifView.xaml.cs
public MainView(Tour tourData)
{
await NotificationRequestHelper.RequestOrder(tourData);
}
NotificationRequestHelper.cs
async public static Task RequestOrder(Tour tour)
{
var notification = new NotificationRequest
{
NotificationId = 100,
Title = "Notif here",
ReturningData = JsonConvert.SerializeObject(tour, Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}),
Schedule =
{
NotifyTime = DateTime.Now.AddSeconds(5) // Used for Scheduling local notification, if not specified notification will show immediately.
}
};
await NotificationCenter.Current.Show(notification);
}
MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
CachedImageRenderer.Init(enableFastRenderer: true);
base.OnCreate(savedInstanceState);
NotificationCenter.CreateNotificationChannel();
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this);
Plugin.InputKit.Platforms.Droid.Config.Init(this, savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
CarouselViewRenderer.Init();
CachedImageRenderer.InitImageViewHandler();
StatusBar.Activity = this;
// set fullscreen
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
Instance = this;
LoadApplication(new App());
Intent.SetFlags(ActivityFlags.SingleTop);
NotificationCenter.NotifyNotificationTapped(Intent);
}
protected override void OnNewIntent(Intent intent)
{
NotificationCenter.NotifyNotificationTapped(intent);
base.OnNewIntent(intent);
}
What I tried to do I found topics which told :
- Add
NotificationCenter.CreateNotificationChannel();andNotificationCenter.NotifyNotificationTapped(Intent);into MainActivity.cs constructor - Override
OnNewIntentaddingNotificationCenter.NotifyNotificationTapped(intent);into MainActivity.cs - Add
LaunchMode = LaunchMode.SingleTopat the top of MainActivity.cs - Add
Intent.SetFlags(ActivityFlags.SingleTop);into MainActivity.cs constructor
Did someone has this issue, and how to fix it ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
