LoginSignup
32
39

More than 5 years have passed since last update.

Windowsで通知用アプリを作ろうとした時に悩んだ諸々

Last updated at Posted at 2019-02-04

作った通知用アプリについて

  • C#を使用する
  • トースト通知(Toast Notification、右下に出てくる短冊状のアレ)で何かを知らせる
  • TCPソケットで通知内容を受信する
  • システムトレイ(System Tray)=タスクトレイ(Task Tray)にアイコンを出し、メニューから終了できる

WPFとUWPって何?

結論

  • WPF:Windowsのデスクトップアプリを作る時に使用するフレームワーク
  • UWP:Windows(Phoneとかも含めた)全般でアプリを動作させるプラットフォーム
  • Win10のデスクトップ用アプリwithシステムトレイはWPFが楽

WPFでどうすればタスクトレイにアイコンを出せるの?

結論

  • Visual Studioでちゃちゃっと出来る

終了機能を作る前に実行しちまった!どうやって消すんだ…

結論

  • タスクマネージャーで消せる
  • Visual Studioから起動したなら、VS下にあります
    • test3.png
  • 普通に起動してもバックグラウンドプロセス下にあります
    • 分かりやすい名前を付けておくと◯

Threadが古い!?Taskって何だよ!

結論

TCP通信が外部PCと繋がらん

結論

  • ファイアウォールでポートを解放
  • Windows Defender ファイアウォールを介したアプリまたは機能を許可で作成したEXEファイルを登録
    • EXEファイルを初めて起動した時にOSが聞いてくれるはず

ToastNotificationManager.CreateToastNotifier(applicationId)のapplicationIdとは

結論

  • Application User Model IDs (AppUserModelIDs)
  • このメソッドを使うときには必須
    • 昔はなくてもトースト通知を発生させられた
    • 自分のアプリのIDは設定しないと存在しないが、他のアプリのを流用可能
    • ゴミ箱 "::{645FF040-5081-101B-9F08-00AA002F954E}"でやるとこんな感じに
      • test2.png
      • test.png
  • 登録済みのID一覧は手軽に取得できる

通知よ、ユーザーが反応するまで消えてくれるな

結論

  • toastのscenarioをreminderに設定すればOK
  • マウスでトーストをクリックするまで残り続けるようになる
  • 以下は2種類の作り方での例
ToastContent content = new ToastContent()
{
    Scenario = ToastScenario.Reminder,
    //(略)
}
<toast scenario="reminder" launch="app-defined-string">
    <!--(略)-->
</toast>

出ない通知

  • エラーログを送りつけたら、通知が発生しない
ASCII文字のみの場合
Unhandled Exception: System.Exception: 通知の内容のサイズが大きすぎます。

通知の内容のサイズが大きすぎます。

   at Windows.UI.Notifications.ToastNotifier.Show(ToastNotification notification)
   at ToastTest.NotifyToaster.Fire(String msg)
マルチバイト文字混じりの場合
Unhandled Exception: System.ArgumentException: Value does not fall within the expected range.
   at Windows.UI.Notifications.ToastNotifier.Show(ToastNotification notification)
   at ToastTest.NotifyToaster.Fire(String msg)

結論

32
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
39