Friday, August 15, 2014

Create Scheduled Toast Notification in Windows Phone Silverlight 8.1

Windows Phone 8.1 has a new feature called Toast Notification where we can show notifications to user and also show it in action center. We can also schedule the toast notification to appear at specific time. We can have scheduled toast notification in both Windows 8.1 Runtime app and Windows 8.1 Silverlight app.

Unlike Windows 8, Windows Phone only has one template for toast notification i.e. “toastText02”. It has application icon on left and accepts two text string with first with bold text and another with normal text. It is advisable to have very short strings to avoid truncation. We can use XML functions to set above two text.


 Dim toastTemplate As ToastTemplateType = ToastTemplateType.ToastImageAndText02
 Dim toastXml As XmlDocument = ToastNotificationManager.GetTemplateContent(toastTemplate)
 Dim toastTextElements As XmlNodeList = toastXml.GetElementsByTagName("text")
 toastTextElements(0).AppendChild(toastXml.CreateTextNode("File Downloaded"))
 toastTextElements(1).AppendChild(toastXml.CreateTextNode("Content.txt file downloaded from server"))

When user clicks on toast notification, the app gets launched. We can set launch attribute of toast element containing parameters which we can use to decide navigation page when the app is launched. We can write following code to set launch parameter.

Dim toastNode = toastXml.SelectSingleNode("/toast")
CType(toastNode, XmlElement).SetAttribute("launch", "?FileID=1")

So if our launch page is MainPage.xaml then uri would be “MainPage.xaml?FileID=1”. We can read the QueryString value in MainPage.xaml and do operation as per project requirement.

Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        MyBase.OnNavigatedTo(e)
        If NavigationContext.QueryString.ContainsKey("FileID") = True Then
            Dim fileID As String = NavigationContext.QueryString("FileID")
            'Perform operation using fileID
       End If
 End Sub

Now we can create a ScheduledToastNotification where we can pass XmlDocument object containing information about toast and scheduled time when the toast notification should be displayed. We can optional also set “ID” property for ScheduledToastNotification so that we can cancel it in future if required.

Dim notification As New ScheduledToastNotification(toastXml, dueTime)
notification.Id = notificationID

We can also set MaximumSnoozeCount parameter to set maximum number of times notification should be displayed and SnoozeInterval parameter to set time interval between notifications. SnoozeInterval should be between 1 to 60 minutes and MaximumSnoozeCount should be between 1 to 5.

Dim notification As New ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromMinutes(5), 3)
If we don’t want to show ToastNotification popup and only show the details in action center then we can set SuppressPopup property.

notification.SuppressPopup = True

It is advisable to check if any toast notification with similar ID is already created or not. If already created then you can notify the user or delete it.

Dim task = ToastNotificationManager.CreateToastNotifier.GetScheduledToastNotifications().Where(Function(f) f.Id = notificationID)
If task.Count > 0 Then
    ToastNotificationManager.CreateToastNotifier.RemoveFromSchedule(task.First)
End If

Now we can create ToastNotifier object and add scheduled notification using following code


ToastNotificationManager.CreateToastNotifier().AddToSchedule(notification)

For ToastNotification to work correctly we have to set Notification Service as “WNS” in WMAppManifest.xml file. This is one of the most important steps as your app could be reject in Certification process if “WNS” is not selected in Notification Service.



Below is complete code to create ScheduledToastNotification in Windows Phone Silverlight 8.1 app.

Private Sub CreateScheduleNotification(ByVal dueTime As Date, ByVal notificationID As Integer)
        Dim toastTemplate As ToastTemplateType = ToastTemplateType.ToastImageAndText02
        Dim toastXml As XmlDocument = ToastNotificationManager.GetTemplateContent(toastTemplate)
        Dim toastTextElements As XmlNodeList = toastXml.GetElementsByTagName("text")
        toastTextElements(0).AppendChild(toastXml.CreateTextNode("File Downloaded"))
        toastTextElements(1).AppendChild(toastXml.CreateTextNode("Content.txt file downloaded from server"))
        Dim toastNode = toastXml.SelectSingleNode("/toast")
        CType(toastNode, XmlElement).SetAttribute("launch", "?FileID=1")
        'Dim notification As New ScheduledToastNotification(toastXml, dueTime)
        Dim notification As New ScheduledToastNotification(toastXml, dueTime, TimeSpan.FromMinutes(5), 3)
        notification.Id = notificationID
        'notification.SuppressPopup =True 'To show notification only in action center
        Dim task = ToastNotificationManager.CreateToastNotifier.GetScheduledToastNotifications().Where(Function(f) f.Id = notificationID)
        If task.Count > 0 Then
            ToastNotificationManager.CreateToastNotifier.RemoveFromSchedule(task.First)
        End If
        ToastNotificationManager.CreateToastNotifier().AddToSchedule(notification)
    End Sub

Wednesday, April 2, 2014

Use WinDBG to see crash dump details

Recently one of my Windows Phone app had a crash and it was available for download in crash count on WP Dev Center. Crash count had an excel file containing links for .CAB file which can be downloaded and we can extract the .CAB file to get the dump file containing stack traces.
We have to use WinDBG which is a free debugging tool from Microsoft. Following are the steps which need to be performed to get the exception details and stack traces.
  1. Download Cab File and extract mdmp file
  2. Open command prompt and set _NT_Symbol_Path and _NT_ALT_Symbol_Path environment variable as below.

    image
  3. As you can see in above image we have set symbol path as environment variable. We have set “http://msdl.microsoft.com/download/symbols” path which is a link for Microsoft Symbol Server.
  4. Open WinDBG using "WinDBG -y" command
  5. Open crash dump file "minidump.mdmp” file from “File -> Open Crash Dump…” (or Ctrl + D key)

    image
  6. As you can see in above image you get a command line at bottom of screen with text “0.000>”.
  7. Turn on the noisy symbol loading mode using “!sym noisy” command. This command instructs the debugger to display information about its search for symbols.
    0.000> !sym noisy
    
    You can find more about this command from following link
  8. Use .reload command to delete all symbol information for the specified module and reload these symbols as needed. Using “/f” parameter with .reload command will force the debugger to load the symbols immediately.
    0.000> .reload /f
    You can know more about this command from following link

    http://msdn.microsoft.com/en-us/library/windows/hardware/ff564805(v=vs.85).aspx

    image
  9. Use “KV” command to get stack frame information. It displays frame pointer omission (FPO) information.
    0.000> kv
    You can find more about this command from following link

    http://msdn.microsoft.com/en-us/library/windows/hardware/ff551943(v=vs.85).aspx

    image
  10. Use “!analyze –v” to get information about current exception or bug check with verbose output.
    0.000> !analyze -v
    You can find more about this command from following link

    http://msdn.microsoft.com/en-us/library/windows/hardware/ff562112(v=vs.85).aspx

    image

Hope above steps helps you finding exception information from dump file available in Dev Center.

Special thanks to Bret Bentzinger and Eric Dunaway for providing lots of valuable input on the debugging process

Other Useful link
http://blogs.msdn.com/b/wsdevsol/archive/2013/08/15/dump-diving-into-cert-failure_3a00_--how-to-get-a-good-callstack-from-triagedump.dmp.aspx