Windows phone 8 在wp8中,应用程序在前台运行时如何获取Toast通知

Windows phone 8 在wp8中,应用程序在前台运行时如何获取Toast通知,windows-phone-8,phonegap-pushplugin,Windows Phone 8,Phonegap Pushplugin,我想在我的windows phone应用程序中实现“toast”通知。我正在实现推送消息,但我希望它们始终显示。无论应用程序是否正在运行。推送通知将在应用程序关闭时处理它,但不会在应用程序运行时处理。如果我手动创建一个shelltoast,它也不会显示。更难的是,我不能使用任何外部dll。我只想使用代码。最好的方法是什么?我已经知道ToastNotificationReceived事件。我想知道如何实现它,这样它就可以在不使用框架的情况下显示类似于“toast”的消息 我的代码在下面 PushP

我想在我的windows phone应用程序中实现“toast”通知。我正在实现推送消息,但我希望它们始终显示。无论应用程序是否正在运行。推送通知将在应用程序关闭时处理它,但不会在应用程序运行时处理。如果我手动创建一个shelltoast,它也不会显示。更难的是,我不能使用任何外部dll。我只想使用代码。最好的方法是什么?我已经知道ToastNotificationReceived事件。我想知道如何实现它,这样它就可以在不使用框架的情况下显示类似于“toast”的消息

我的代码在下面

PushPlugin.cs(c#代码)

在javascript中

function onNotificationWP8(data) {
    var pushNotification;
    pushNotification = window.plugins.pushNotification;
    pushNotification.showToastNotification(successHandler, errorHandler,
      {
          "Title": data.jsonContent["wp:Text1"], "Content": data.jsonContent["wp:Text2"], "NavigationUri": data.jsonContent["wp:Param"]
      });

}

在没有Windows Phone 8 Update 3的设备上,当目标应用程序在前台运行时,不会显示toast通知。在使用Windows Phone 8 Update 3的设备上,当目标应用程序在前台运行时,会显示toast通知,但会被其他活动(如电话或锁定屏幕)遮挡

下面的C代码示例显示了用于使用本地代码创建toast通知的属性

// Create a toast notification.
// The toast notification will not be shown if the foreground app is running.
ShellToast toast = new ShellToast();
toast.Title = "[title]";
toast.Content = "[content]";
toast.Show();

这就是您在没有Windows Phone 8 Update 3的设备上查找的所有内容,当目标应用程序在前台运行时,不会显示toast通知。在使用Windows Phone 8 Update 3的设备上,当目标应用程序在前台运行时,会显示toast通知,但会被其他活动(如电话或锁定屏幕)遮挡

public static class Notification
{
    public static string ChannelURI = string.Empty;

    public static void MainNotificationCallFunction()
    {
        try
        {
            NotificationMessage("Test Notification");
        }
        catch (Exception e)
        { }
    }

    public static void NotificationMessage(string Message)
    {
        try
        {
            ToastTemplateType toastType = ToastTemplateType.ToastText02;

            XmlDocument toastXmlJob = ToastNotificationManager.GetTemplateContent(toastType);
            XmlNodeList toastTextElementJob = toastXmlJob.GetElementsByTagName("text");
            toastTextElementJob[0].AppendChild(toastXmlJob.CreateTextNode(Message));
            IXmlNode toastNodeJob = toastXmlJob.SelectSingleNode("/toast");
            ((XmlElement)toastNodeJob).SetAttribute("duration", "long");
            ToastNotification toastJob = new ToastNotification(toastXmlJob);
            ToastNotificationManager.CreateToastNotifier().Show(toastJob);
        }
        catch (Exception e)
        { }
    }

    public static void PushNotification()
    {
        try
        {
            /// Holds the push channel that is created or found.
            HttpNotificationChannel pushChannel;
            string channelName = "Usman's Channel";

            // Try to find the push channel.
            pushChannel = HttpNotificationChannel.Find(channelName);

            // If the channel was not found, then create a new connection to the push service.
            if (pushChannel == null)
            {
                pushChannel = new HttpNotificationChannel(channelName);

                //// Register for all the events before attempting to open the channel.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

                pushChannel.Open();
                pushChannel.BindToShellTile();
                pushChannel.BindToShellToast();
            }
            else
            {
                //// The channel was already open, so just register for all the events.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
            }
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
                System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
                MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri.ToString()));

            });
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        try
        {
            // Error handling logic for your particular application would be here.
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData)));
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
    {
        try
        {
            string message;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
            {
                message = reader.ReadToEnd();
            }

            Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(String.Format("Received Notification {0}:\n{1}", DateTime.Now.ToShortTimeString(), message)));
        }
        catch (Exception ex)
        { }
    }

    private static void channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK);
            });
        }
        catch (Exception ex)
        { }
    }
    private static void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                //ProgressBarPushNotifications.Visibility = System.Windows.Visibility.Collapsed;
                MessageBox.Show(e.ChannelUri.ToString(), "Uri Recieved", MessageBoxButton.OK);
            });
        }
        catch (Exception ex)
        { }
    }
    private static void channel_ShellToastNotificationReceived(object sender, HttpNotificationEventArgs e)
    {
        try
        {
            StringBuilder message = new StringBuilder();
            string relativeUri = string.Empty;

            message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

            using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
            {
                message.AppendFormat(reader.ReadToEnd());
            }

            // Display a dialog of all the fields in the toast.
            Deployment.Current.Dispatcher.BeginInvoke(() => 
            {
                MessageBox.Show(message.ToString());
            });
        }
        catch (Exception ex)
        { }
    }
}
下面的C代码示例显示了用于使用本地代码创建toast通知的属性

// Create a toast notification.
// The toast notification will not be shown if the foreground app is running.
ShellToast toast = new ShellToast();
toast.Title = "[title]";
toast.Content = "[content]";
toast.Show();
这就是您想要的全部

公共静态类通知
public static class Notification
{
    public static string ChannelURI = string.Empty;

    public static void MainNotificationCallFunction()
    {
        try
        {
            NotificationMessage("Test Notification");
        }
        catch (Exception e)
        { }
    }

    public static void NotificationMessage(string Message)
    {
        try
        {
            ToastTemplateType toastType = ToastTemplateType.ToastText02;

            XmlDocument toastXmlJob = ToastNotificationManager.GetTemplateContent(toastType);
            XmlNodeList toastTextElementJob = toastXmlJob.GetElementsByTagName("text");
            toastTextElementJob[0].AppendChild(toastXmlJob.CreateTextNode(Message));
            IXmlNode toastNodeJob = toastXmlJob.SelectSingleNode("/toast");
            ((XmlElement)toastNodeJob).SetAttribute("duration", "long");
            ToastNotification toastJob = new ToastNotification(toastXmlJob);
            ToastNotificationManager.CreateToastNotifier().Show(toastJob);
        }
        catch (Exception e)
        { }
    }

    public static void PushNotification()
    {
        try
        {
            /// Holds the push channel that is created or found.
            HttpNotificationChannel pushChannel;
            string channelName = "Usman's Channel";

            // Try to find the push channel.
            pushChannel = HttpNotificationChannel.Find(channelName);

            // If the channel was not found, then create a new connection to the push service.
            if (pushChannel == null)
            {
                pushChannel = new HttpNotificationChannel(channelName);

                //// Register for all the events before attempting to open the channel.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

                pushChannel.Open();
                pushChannel.BindToShellTile();
                pushChannel.BindToShellToast();
            }
            else
            {
                //// The channel was already open, so just register for all the events.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
            }
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
                System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
                MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri.ToString()));

            });
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        try
        {
            // Error handling logic for your particular application would be here.
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData)));
        }
        catch (Exception ex)
        { }
    }
    private static void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
    {
        try
        {
            string message;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
            {
                message = reader.ReadToEnd();
            }

            Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(String.Format("Received Notification {0}:\n{1}", DateTime.Now.ToShortTimeString(), message)));
        }
        catch (Exception ex)
        { }
    }

    private static void channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK);
            });
        }
        catch (Exception ex)
        { }
    }
    private static void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
        try
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                //ProgressBarPushNotifications.Visibility = System.Windows.Visibility.Collapsed;
                MessageBox.Show(e.ChannelUri.ToString(), "Uri Recieved", MessageBoxButton.OK);
            });
        }
        catch (Exception ex)
        { }
    }
    private static void channel_ShellToastNotificationReceived(object sender, HttpNotificationEventArgs e)
    {
        try
        {
            StringBuilder message = new StringBuilder();
            string relativeUri = string.Empty;

            message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

            using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
            {
                message.AppendFormat(reader.ReadToEnd());
            }

            // Display a dialog of all the fields in the toast.
            Deployment.Current.Dispatcher.BeginInvoke(() => 
            {
                MessageBox.Show(message.ToString());
            });
        }
        catch (Exception ex)
        { }
    }
}
{ 公共静态字符串ChannelURI=string.Empty; 公共静态void MainNotificationCallFunction() { 尝试 { 通知消息(“测试通知”); } 捕获(例外e) { } } 公共静态无效通知消息(字符串消息) { 尝试 { ToastTemplateType toastType=ToastTemplateType.ToastText02; XmlDocument toastXmlJob=ToastNotificationManager.GetTemplateContent(toastType); XmlNodeList toastTextElementJob=toastXmlJob.GetElementsByTagName(“文本”); toastTextElementJob[0].AppendChild(toastXmlJob.CreateTextNode(消息)); IXmlNode toastNodeJob=toastXmlJob.SelectSingleNode(“/toast”); ((xmlement)toastNodeJob.SetAttribute(“duration”、“long”); ToastNotification toastJob=新的ToastNotification(toastXmlJob); ToastNotificationManager.CreateToastNotifier().Show(toastJob); } 捕获(例外e) { } } 公共静态无效通知() { 尝试 { ///保存已创建或找到的推送通道。 HttpNotificationChannel推送通道; 字符串channelName=“乌斯曼频道”; //试着找到推送通道。 pushChannel=HttpNotificationChannel.Find(channelName); //如果未找到通道,则创建到推送服务的新连接。 if(pushChannel==null) { pushChannel=新的HttpNotificationChannel(通道名称); ////在尝试打开通道之前注册所有事件。 pushChannel.ChannelUriUpdated+=新事件处理程序(pushChannel\u ChannelUriUpdated); pushChannel.ErrorOccursed+=新的事件处理程序(pushChannel\u ErrorOccursed); pushChannel.HttpNotificationReceived+=新事件处理程序(pushChannel\u HttpNotificationReceived); pushChannel.Open(); pushChannel.BindToShellTile(); pushChannel.BindToShellToast(); } 其他的 { ////频道已经打开,所以只需注册所有事件。 pushChannel.ChannelUriUpdated+=新事件处理程序(pushChannel\u ChannelUriUpdated); pushChannel.ErrorOccursed+=新的事件处理程序(pushChannel\u ErrorOccursed); pushChannel.HttpNotificationReceived+=新事件处理程序(pushChannel\u HttpNotificationReceived); } } 捕获(例外情况除外) { } } 私有静态void PushChannel\u ChannelUriUpdated(对象发送方,NotificationChannelUriEventArgs e) { 尝试 { Deployment.Current.Dispatcher.BeginInvoke(()=> { //出于测试目的显示新的URI。通常,URI会在此时传递回web服务。 System.Diagnostics.Debug.WriteLine(例如ChannelUri.ToString()); Show(String.Format(“通道Uri为{0}”,e.ChannelUri.ToString()); }); } 捕获(例外情况除外) { } } 发生私有静态无效PushChannel_错误(对象发送方,NotificationChannelErrorEventArgs e) { 尝试 { //这里将显示特定应用程序的错误处理逻辑。 Deployment.Current.Dispatcher.BeginInvoke(()=> Show(String.Format(“发生推送通知{0}错误。{1}({2}){3}”,e.ErrorType,e.Message,e.ErrorCode,e.ErrorAdditionalData)); } 捕获(例外情况除外) { } } 私有静态无效PushChannel_HttpNotificationReceived(对象发送方,HttpNotificationEventArgs e) { 尝试 { 字符串消息; 使用(System.IO.StreamReader=new System.IO.StreamReader(e.Notification.Body)) { message=reader.ReadToEnd(); } Deployment.Current.Dispatcher.BeginInvoke(()=>MessageBox.Show(String.Format(“已接收通知{0}:\n{1}”),DateTime.Now.ToShortTimeStri