Xamarin.Macで通知センターを使用する

提供: MonoBook
2017年10月23日 (月) 05:20時点における162.158.179.205 (トーク)による版 (ページの作成:「<source lang="csharp"> var userNotifycationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter; // 通知がクリックされたと...」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動
            var userNotifycationCenter = NSUserNotificationCenter.DefaultUserNotificationCenter;

            // 通知がクリックされたとき
            userNotifycationCenter.DidActivateNotification += (sender, e) => {
                Console.WriteLine("DidActivateNotification: " + e.Notification);
            };

            // 通知されたとき
            userNotifycationCenter.DidDeliverNotification += (sender, e) => {
                Console.WriteLine("DidDeliverNotification");
            };

            // create
            var userNotification = new NSUserNotification();
            userNotification.Title = "タイトル";
            userNotification.Subtitle = "サブタイトル";
            userNotification.InformativeText = "本文";
            userNotification.UserInfo = NSDictionary.FromObjectsAndKeys(new[] { "val" }, new[] { "key" });

            // 10秒後に通知
            userNotification.DeliveryDate = NSDate.Now.AddSeconds(10);

            // 通知実行
            //userNotifycationCenter.DeliverNotification(un);
            userNotifycationCenter.ScheduleNotification(userNotification);

macOSの標準設定では3分以内に同じ通知を繰り返し送信するとポップアップ表示されない(右上の通知アイコンをクリックすると一覧にはいる)。 これを知らずポップアップ通知されないバグだと思ってググってしまった。