简体   繁体   中英

How to display push notification for user in windows 8.1?

I am currently working on Windows 8.1 Push Notification part. I have read different links and found that first we need to register the app and get all the information like SID and Client Secret and send to our server team so they can send push notification.

Then after this, I implemented the following code at my side to get channelUri and Expiration date of that Uri from WNS.

  PushNotificationChannel channel = null;
        try
        {
            channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
            if (channel != null)
            {
                var notificationUri = channel.Uri;
                var expiration_time = channel.ExpirationTime;
            }
            channel.PushNotificationReceived += channel_PushNotificationReceived;
        }
        catch (Exception ex)
        {
            if (ex != null)
            {
                System.Diagnostics.Debug.WriteLine(ex.HResult);
            }
        } 

I have received all the values perfectly and my server team added a logic to send me push notification. Now, the problem which I am facing is that I am not aware how to display the received push notification sent by server to that user. Also, can we display the notification is the app is not running or is in background?

Background Tasks solved my problem.

First you need to create a WindowsRuntimeComponent Project and add the code below

public sealed class PushNotification:IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails as RawNotification;
        if (notification != null)
        {
            ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
            var textElemets = toastXml.GetElementsByTagName("text");
            textElemets[0].AppendChild(toastXml.CreateTextNode(notification.Content));
            var imageElement = toastXml.GetElementsByTagName("image");
            imageElement[0].Attributes[1].NodeValue = "ms-appx:///Assets/50.png";
            ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
        }

    }
}

Then register the background task in any of the page( i added in Home Page) using below code

private async void RegisterBackgroundTask()
    {

        await BackgroundExecutionManager.RequestAccessAsync();

        try
        {

            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                try
                {
                    task.Value.Unregister(false);
                }
                catch
                {
                    //
                }
            }
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
            builder.Name = "Push Notifcation Task";
            builder.TaskEntryPoint = typeof(PushNotification).FullName;
            builder.SetTrigger(new PushNotificationTrigger());
            builder.Register();
        }
        catch(Exception e)
        {
            if(e != null)
            {
                System.Diagnostics.Debug.WriteLine(e.HResult);
                System.Diagnostics.Debug.WriteLine(e.InnerException);
            }
        }
    }

Please don't forget to add this background task in Declarations section in Package.appmanifest file and name of Entry Point should match with builder.TaskEntryPoint = typeof(PushNotification).FullName; else you will get exception.

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM