2016-04-28 10 views
1

Windowsの電話アプリケーション内に「トースト」通知を実装したいと思います。私はプッシュメッセージを実装していますが、それらを常に表示します。アプリケーションが実行されているかどうかに関係なくプッシュ通知は、アプリケーションが閉じているときに処理しますが、実行中では処理しません。また、私が手動でシェルトゥースを作成しても表示されません。それをより困難にするために、私は外部dllを使用することはできません。私はコードを使いたいだけです。これを行う最善の方法は何でしょうか?私はすでにToastNotificationRecievedイベントについて知っています。それはフレームワークを使用せずにメッセージのような「乾杯」と表示されますように、私は私のコードはアプリがwp8でフォアグラウンドで実行されているときにトースト通知を取得する方法

PushPlugin.cs(C#のコード)で

public void showToastNotification(string options) 
    { 

     ShellToast toast; 
     if (!TryDeserializeOptions(options, out toast)) 
     { 
      this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); 
      return; 
     } 
     Deployment.Current.Dispatcher.BeginInvoke(toast.Show); 

    } 



public void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e) 
     { 
var toast = new PushNotification 
      { 
       Type = "toast" 
      }; 

      foreach (var item in e.Collection) 
      { 
       toast.JsonContent.Add(item.Key, item.Value); 
      } 

      this.ExecuteCallback(this.pushOptions.NotificationCallback, JsonConvert.SerializeObject(toast)); 
     } 

を下回っている

それを実装する方法を知りたいですjavacript

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"] 
     }); 

} 

答えて

0

Windows Phone 8 Update 3がインストールされていないデバイスでは、ターゲットアプリケーションがフォアグラウンドで実行されているときにトースト通知が表示されません。 Windows Phone 8 Update 3のデバイスでは、ターゲットアプリケーションがフォアグラウンドで実行されているときにトースト通知が表示されますが、電話やロック画面などの他のアクティビティでは表示されません。

次のC#コード例は、ローカルコードを使用してトースト通知を作成するために使用されるプロパティを示しています。

// 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(); 

これthreadは、それはすべてあなたが私のコードDeployment.Current.Dispatcher.BeginInvoke(toast.Show)で

+0

を探しています。実際にトーストは表示されません。 – Abi

0
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) 
     { } 
    } 
} 
関連する問題