edsnider / localnotificationsplugin

Local Notifications Plugin for Xamarin and Windows
MIT License
198 stars 82 forks source link

Notification click event #60

Open stesvis opened 5 years ago

stesvis commented 5 years ago

Is there a way to handle the notification click event? If not....

Feature Request

In 99.9% of the cases, when a local notification is fired we need to do something. It would be hugely helpful to add a LocalNotificationReceived event handler.

CrossLocalNotifications.Current.LocalNotificationReceived += OnLocalNotificationReceived;

...

private void OnLocalNotificationReceived(object sender, LocalNotificationReceivedEventArgs e)
{
    var id = e.Id;
    var title = e.Title; 
    var message = e.Body; 
}
ianvink commented 5 years ago

in iOS add this to your AppDelegate.cs

    public async override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {

    }
Max87ZA commented 5 years ago

Hi guys, is there any chance to have this implemented on Android? I need to open app when user clicks on notification and display an alert controller. Thanks

mrehman29 commented 5 years ago

on Android you can add

protected override void OnNewIntent(Intent intent)
        {
        }

and if you are passing some data with your notification you can do something like

On Android

protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            var fromid = intent.GetStringExtra("fromid");
            if (fromid != null && fromid != "")
            {
                MessagingCenter.Send<object, string>(this, "PushToChat", intent.GetStringExtra("fromid"));
            }
            System.Diagnostics.Debug.WriteLine("Exta fromid: "+intent.GetStringExtra("fromid"));
            System.Diagnostics.Debug.WriteLine("Exta Time: " + intent.GetStringExtra("datetime"));
            System.Diagnostics.Debug.WriteLine("Exta message: " + intent.GetStringExtra("message"));
        }

On IOS

[Export("application:didReceiveLocalNotification:")]
        public void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
        {

            var userdata = notification.UserInfo;
            var fromid = userdata.ValueForKey(new NSString("fromid")).ToString();
            if (fromid != null && fromid != "")
            {
                MessagingCenter.Send<object, string>(this, "PushToChat", userdata.ValueForKey(new NSString("fromid")).ToString());
            }

        }