Can I create an alarm app for Window Universal App?

末鹿安然 提交于 2019-12-03 00:40:09

Windows-universal-samples has recently been updated with a few new RTM samples including this one - Notifications.

As Alarm is also one type of notification, it's now built within a new toast notification framework in the Universal Windows Platform.

After you downloaded the source code from the Notification's link above, run it with Visual Studio 2015 RTM and then once the app is loaded, go to

toasts > scenarios > scenario: alarm

and you will see a fully functional alarm app (along with Reminder and a lot other samples).


Let's talk about code.

Basically, unlike in Windows Phone Silverlight, you can now customise the alarm popup a bit by specifying the xml payload like this (make sure the scenario is set to alarm)

<toast launch='args' scenario='alarm'>
    <visual>
        <binding template='ToastGeneric'>
            <text>Alarm</text>
            <text>Get up now!!</text>
        </binding>
    </visual>
    <actions>

        <action arguments = 'snooze'
                content = 'snooze' />

        <action arguments = 'dismiss'
                content = 'dismiss' />

    </actions>
</toast>

And then create an XmlDocument which loads the above xml string

var xmlString = @"//copy above xml here//";
var doc = new Windows.Data.Xml.Dom.XmlDocument();
doc.LoadXml(xmlString);

Then create a ToastNotification and trigger it with ToastNotificationManager-

var toast = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(toast);

That's it! You will see an alarm popup like below.


Update

Microsoft recently responded to one of my API requests and I am posting the content here so everyone knows what APIs have been added and what are still outstanding.

What has been done

  1. There is now a way to create an alarm/reminder in universal windows apps;
  2. The alarm/reminder supports custom snooze time (you can choose to let system handle snooze, or wake up your background task to do it manually);
  3. The alarm/reminder supports vibrate only (just like toast) that can be overwritten by user to turn off vibration;
  4. The alarm/reminder supports a good level of customizability (custom optional inline image, custom additional actions, etc).

Some references

What we (MSFT) know that’s missing and hope to support in the near future

  1. Native platform support in alarm/reminder to automatically handle time conversion when time-zone changes (Workaround – this can be done by the app manually by using the TimeZoneChange system trigger);
  2. Native platform support in alarm/reminder for recurrence events (Workaround – this can currently only be done by the app manually periodically waking up and reschedule a bunch of alarms/reminders ahead of time);

  3. Native platform support to select a song from Music library as ring tone for alarm/reminder (Workaround – this can be done by reading and copying files from your music library, and then use the saved/modified version of the file in your app package or app data as the ring tone (toast notification supports custom sound by pointing to files in appx or appdata in the xml payload)).

AlarmApplicationManager can be used to create alarm apps. It gives capabilities to schedule toast notifications.

var scheduledToast = new ScheduledToastNotification(content, DateTime.Now.AddMinutes(5));
toastNotifier.AddToSchedule(scheduledToast);

An audio source can also be set while creating the toast template, but only from a set of predefined sounds supplied by windows.

Refer AlarmApplicationManager and Building alarm app for more details.

There are a number of Win 10 Universal Samples on GitHub which may be useful. I didn't see anything directly related to Alarms, though.

Rafael Regh

unfortunately Windows Universal Applications have no direct access to the Display Settings. But you can use the AlarmApplicationManager Class to create an Alarm. This will, in some cases (for sure on WindowsPhone) turn on the display automatically to show off the Alarm (with Title and Description).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!