How to handle local notifications on Windows Phone

I'm having problems with navigation after user taps a local notification. My goal is to navigate user to specific view after he taps a notification. I already managed to make it work on both Android and iOS but I have problems with Windows Phone. As far I know on Windows Phone I can navigate between views using URIs. Class ScheduledToastNotification as a parameter gets a XML template of notification. I can set the 'launch' attribute of that XML with URI which should navigate me to specific view. I have two problems however:

  • In my Windows Phone project I have only MainPage.xaml view that I can navigate to. All other Views are contained in PCL and I don't know if it is possible to provide URI that points to View in PCL.

  • If 'launch' parameter of XML template is set and application is running (no matter if it is in foreground on background) I get an exception after notification is tapped.
    Click

Code that I use to set notification on Windows Phone:

public void Notify(INotification notification)
{
    var _notifier = ToastNotificationManager.CreateToastNotifier();
    var _toastNotificationSchema = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

    var toastText = _toastNotificationSchema.GetElementsByTagName("text");

    ((XmlElement)toastText[0]).InnerText = notification.NotificationText;
    IXmlNode toastNode = _toastNotificationSchema.SelectSingleNode("/toast");
    ((XmlElement)toastNode).SetAttribute("launch", string.Format("/MainPage.xaml?notificationID={0}", notification.ID));

    var scheduledNotification = new ScheduledToastNotification(_toastNotificationSchema,
                                                               notification.FireDateTime,
                                                               TimeSpan.FromSeconds(60),
                                                               5)
                                {Id = notification.ID.ToString()};

    _notifier.AddToSchedule(scheduledNotification);
}

I think I could somehow hack solution of my issue if I would be able to set Query String and then access if after app is brought to foreground after user taps notification. I don't know if it is possible though.

Is there any way I can achieve navigation using local notifications on Windows Phone? I know that push notifications would solve all my problems but unfortunately I cannot use that.

Answers

  • DonaldJansenDonaldJansen USMember

    I too have the same problem. I cannot find a way to solve it

  • veerooveeroo ✭✭ USMember ✭✭

    Me too

  • DonaldJansenDonaldJansen USMember

    With me sometimes when the app is alive the app gets reopened twice instead of one instance, and as soon as I close the one (by pressing back button) I get that exact same Exception

  • LukaszStarzykLukaszStarzyk USMember

    Reopening of app is a result of something that is called "Fast Resume". It basically creates new instance of Page that is pushed on top of existing navigation stack. But because with Xamarin we have only one Page which initializes everything we get effect of application launching second time. I tried to disable Fast Resume by editing WPAppManifest.xml but it didn't solve the problem.

    I don't know if it is possible to achieve behavior we want on Windows Phone. For me problem would be solved if I could handle user tapping notification bar event. Unfortunately local notifications on Windows Phone don't offer such feature, it is only possible with push notifications.

  • DonaldJansenDonaldJansen USMember

    Then for now untill there is another Solution I will make use of a NotificaitonId and test if it was already handled. a hacky way but should work, for now atleast

Sign In or Register to comment.