none
[WP8.1]Toast notifications for Windows Phone 8.1 with custom sound

    Question

  • Hello,

    I'm trying to implement an option for my application to play custom sounds from local storage when the toast notification appears. In this case, I mean not the package installed folder, but
    ApplicationData.Current.LocalFolder

    . I think, that I must somehow play with URI's. I already asked this question here: http://stackoverflow.com/questions/34180143/windows-phone-8-1-local-storage-file-for-toast-notification , but no luck. Does anybody know the solution for this?

    Saturday, December 12, 2015 1:37 PM

All replies

  • Hi FallenAngel9,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    In order to let the toast notifications for Windows Phone 8.1 using the custom sound which is stored in the ApplicationData.Current.LocalFolder, we need to use the ms-appdata:///local/.. address and I have created the following two examples, both of them work fine.

    Example One:
    In the MainPage.xaml.cs:

          private void Button_Click(object sender, RoutedEventArgs e)
            {
                ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
                XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode("Test1"));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode("Test2"));
                IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
                XmlElement audio = toastXml.CreateElement("audio");
                string sound = "test1.mp3";
                if (sound != null)
                    audio.SetAttribute("src", "ms-appdata:///local/" + sound);
                else
                    audio.SetAttribute("src", "ms-appx:///sounds/Bell.mp3");
                toastNode.AppendChild(audio);
                ToastNotifier toastNotifier =
                 ToastNotificationManager.CreateToastNotifier();
                ToastNotification toastNotification = new ToastNotification(toastXml);
                toastNotifier.Show(toastNotification);
            }

    Example Two:

    In the XMLFile1.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <toast>
      <visual>
        <binding template="ToastText02">
          <text id="1">Test1</text>
          <text id="2">Test2</text>
        </binding>
      </visual>
    <audio src="ms-appdata:///local/test1.mp3"/>
    </toast>
    In the MainPage.xaml.cs:
     private void Button_Click(object sender, RoutedEventArgs e)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(XDocument.Load("XMLFile1.xml").ToString());
                ToastNotifier toastNotifier =
                ToastNotificationManager.CreateToastNotifier();
                ToastNotification toastNotification = new ToastNotification(xmldoc);
                toastNotifier.Show(toastNotification);
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, December 14, 2015 4:20 AM
    Owner
  • Hello Amy Peng,

    Thanks for your piece of advice, I will keep that in mind, when I will ask something on forums.

    I checked your code and mine, and I have a question - does your code allow to use the dynamically created audio file for the notification? Because I try with the following code and it doesn't allow:

    I have a global variable of StorageFile type

    StorageFile sound;

    Here's the toast notification implementation:

    private void Button_Click(objectsender, RoutedEventArgse)

    {

    ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXml.CreateTextNode("title")); toastTextElements[1].AppendChild(toastXml.CreateTextNode("Main Message")); IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); XmlElement audio = toastXml.CreateElement("audio"); if(sound!=null) audio.SetAttribute("src", "ms-appdata:///local/"+sound.Name); else audio.SetAttribute("src", "ms-appx:///sounds/Bell.mp3"); toastNode.AppendChild(audio);

    ScheduledToastNotification toast = new ScheduledToastNotification(toastXml, time);/*time is DateTime variable*/

    ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);

    And here is the piece of code, which is invoked, when the button is tapped:

    private async void SoundRec_Tapped(object sender, TappedRoutedEventArgs e) {

    var folder = ApplicationData.Current.LocalFolder; sound = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///sounds/Bell.mp3")); await sound.CopyAsync(folder);

    }

    As you can see, the code is nearly the same, and what is pretty strange, the file is truly in the ApplicationData.Current.LocalFolder , and can be accessed by the StorageFile.GetFileFromApplicationUriAsync()  from the scheme ms-appdata:///, but it can't be accessed by the toast notification. It just plays the standard Windows Notification sound. But if the custom sound is located at the application installation folder, the sound can be played via the uri scheme  ms-appx:///


    Monday, December 14, 2015 9:03 AM
  • Hi FallenAngel9,

    I have tested your code again, but it works fine in my side. So I still can not reproduce your issue in my side.
    Could please post your project in here? I want to have a test to see if some others code affect it.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 16, 2015 8:58 AM
    Owner
  • Hello Amy Peng,

    Yes, but can I upload only problem code, due to keeping the source code safe? At any case, here's the archive with that problem part of application : https://dropmefiles.com/remGq
    Thursday, December 17, 2015 4:44 PM
  • Hi FallenAngel9,

    I am sorry I can not find your project in this link(https://dropmefiles.com/remGq). Could you please try to upload your project in the OneDrive?

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 5, 2016 6:15 AM
    Owner
  • Hello Amy Peng,

    Yes, finally I understood, how to share a link :)
    Here it is: https://onedrive.live.com/redir?resid=90B6478DC2E1D0BD!332864&authkey=!AA0z8vqaLmVkaFU&ithint=file%2czip
    Thursday, January 7, 2016 6:59 AM
  • I do not have the windows phone 8.1, I test it on windows 10, it seems that all works fine.
    Tuesday, January 12, 2016 2:50 AM
  • Hello Mankdng Nef,

    Just to be sure, that I haven't made any mistake, I've created a new project with the same idea. But it doesn't work too. I'm testing it on Lumia 640 WP8.1 . Here's the link to this new project
    Sunday, January 17, 2016 9:17 AM
  • So it is unresolvable problem? Windows Phone just can't use sounds from local storage?
    Friday, February 5, 2016 2:37 PM
  • Today Microsoft released Windows 10. Maybe, this issue can gone in new OS. But I have Lumia 530, which can be left without upgrade. So, if this issue won't be resolved in 8.1 , the Store won't get any new applications from me, just because I could not implement that technology. -.-
    Thursday, March 17, 2016 7:34 PM
  • Hi, I have the same problem. Some of my WP8.1 customers have problems with custom sounds in notifications, some not. And I can't reproduce it with my own Lumia 925 (WP8.1)...

    Are there any news?

    Thx,

    Alex

    Sunday, June 11, 2017 12:02 PM