OSDN Git Service

Pushbulletを利用したスマホへのプッシュ通知を実装する
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Mon, 15 May 2017 13:13:17 +0000 (22:13 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Tue, 16 May 2017 13:40:28 +0000 (22:40 +0900)
KancolleSniffer/Config.cs
KancolleSniffer/ConfigDialog.Designer.cs
KancolleSniffer/ConfigDialog.cs
KancolleSniffer/KancolleSniffer.csproj
KancolleSniffer/MainForm.cs
KancolleSniffer/NotificationConfigDialog.Designer.cs
KancolleSniffer/NotificationConfigDialog.cs
KancolleSniffer/PushBullet.cs [new file with mode: 0644]

index bb215e8..469f9ea 100644 (file)
@@ -73,6 +73,12 @@ namespace KancolleSniffer
         public string Token { get; set; } = "";\r
     }\r
 \r
+    public class PushbulletConfig\r
+    {\r
+        public bool On { get; set; }\r
+        public string Token { get; set; } = "";\r
+    }\r
+\r
     public class SoundConfig\r
     {\r
         public int Volume { get; set; } = 100;\r
@@ -103,7 +109,8 @@ namespace KancolleSniffer
         FlashWindow = 1,\r
         ShowBaloonTip = 1 << 1,\r
         PlaySound = 1 << 2,\r
-        All = (1 << 3) - 1\r
+        Pushbullet = 1 << 3,\r
+        All = (1 << 3) - 1 // Pushbullet以外\r
     }\r
 \r
     public class NotificationConfig\r
@@ -144,6 +151,7 @@ namespace KancolleSniffer
         public ShipListConfig ShipList { get; set; } = new ShipListConfig();\r
         public LogConfig Log { get; set; } = new LogConfig();\r
         public KancolleDbConfig KancolleDb { get; set; } = new KancolleDbConfig();\r
+        public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig();\r
 \r
         public static readonly string[] NotificationNames =\r
         {\r
index b1d52b3..60c1e4b 100644 (file)
@@ -106,6 +106,11 @@ namespace KancolleSniffer
             this.label11 = new System.Windows.Forms.Label();\r
             this.buttonOutputDir = new System.Windows.Forms.Button();\r
             this.tabPageCollabo = new System.Windows.Forms.TabPage();\r
+            this.groupBoxPushbullet = new System.Windows.Forms.GroupBox();\r
+            this.buttonPushbulletTest = new System.Windows.Forms.Button();\r
+            this.textBoxPushbulletToken = new System.Windows.Forms.TextBox();\r
+            this.label7 = new System.Windows.Forms.Label();\r
+            this.checkBoxPushbulletOn = new System.Windows.Forms.CheckBox();\r
             this.groupBoxKancolleDb = new System.Windows.Forms.GroupBox();\r
             this.textBoxKancolleDbToken = new System.Windows.Forms.TextBox();\r
             this.label8 = new System.Windows.Forms.Label();\r
@@ -139,6 +144,7 @@ namespace KancolleSniffer
             this.tabPageLog.SuspendLayout();\r
             ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaterialLogInterval)).BeginInit();\r
             this.tabPageCollabo.SuspendLayout();\r
+            this.groupBoxPushbullet.SuspendLayout();\r
             this.groupBoxKancolleDb.SuspendLayout();\r
             this.tabPageDebug.SuspendLayout();\r
             this.SuspendLayout();\r
@@ -814,6 +820,7 @@ namespace KancolleSniffer
             // \r
             // tabPageCollabo\r
             // \r
+            this.tabPageCollabo.Controls.Add(this.groupBoxPushbullet);\r
             this.tabPageCollabo.Controls.Add(this.groupBoxKancolleDb);\r
             this.tabPageCollabo.Location = new System.Drawing.Point(4, 40);\r
             this.tabPageCollabo.Name = "tabPageCollabo";\r
@@ -822,6 +829,56 @@ namespace KancolleSniffer
             this.tabPageCollabo.Text = "連携";\r
             this.tabPageCollabo.UseVisualStyleBackColor = true;\r
             // \r
+            // groupBoxPushbullet\r
+            // \r
+            this.groupBoxPushbullet.Controls.Add(this.buttonPushbulletTest);\r
+            this.groupBoxPushbullet.Controls.Add(this.textBoxPushbulletToken);\r
+            this.groupBoxPushbullet.Controls.Add(this.label7);\r
+            this.groupBoxPushbullet.Controls.Add(this.checkBoxPushbulletOn);\r
+            this.groupBoxPushbullet.Location = new System.Drawing.Point(11, 66);\r
+            this.groupBoxPushbullet.Name = "groupBoxPushbullet";\r
+            this.groupBoxPushbullet.Size = new System.Drawing.Size(234, 48);\r
+            this.groupBoxPushbullet.TabIndex = 1;\r
+            this.groupBoxPushbullet.TabStop = false;\r
+            this.groupBoxPushbullet.Text = "Pushbullet";\r
+            // \r
+            // buttonPushbulletTest\r
+            // \r
+            this.buttonPushbulletTest.Font = new System.Drawing.Font("MS ゴシック", 9F);\r
+            this.buttonPushbulletTest.Location = new System.Drawing.Point(191, 16);\r
+            this.buttonPushbulletTest.Name = "buttonPushbulletTest";\r
+            this.buttonPushbulletTest.Size = new System.Drawing.Size(37, 23);\r
+            this.buttonPushbulletTest.TabIndex = 3;\r
+            this.buttonPushbulletTest.Text = "Test";\r
+            this.buttonPushbulletTest.UseVisualStyleBackColor = true;\r
+            this.buttonPushbulletTest.Click += new System.EventHandler(this.buttonPushbulletTest_Click);\r
+            // \r
+            // textBoxPushbulletToken\r
+            // \r
+            this.textBoxPushbulletToken.Location = new System.Drawing.Point(99, 18);\r
+            this.textBoxPushbulletToken.Name = "textBoxPushbulletToken";\r
+            this.textBoxPushbulletToken.Size = new System.Drawing.Size(89, 19);\r
+            this.textBoxPushbulletToken.TabIndex = 2;\r
+            // \r
+            // label7\r
+            // \r
+            this.label7.AutoSize = true;\r
+            this.label7.Location = new System.Drawing.Point(57, 21);\r
+            this.label7.Name = "label7";\r
+            this.label7.Size = new System.Drawing.Size(42, 12);\r
+            this.label7.TabIndex = 1;\r
+            this.label7.Text = "トークン:";\r
+            // \r
+            // checkBoxPushbulletOn\r
+            // \r
+            this.checkBoxPushbulletOn.AutoSize = true;\r
+            this.checkBoxPushbulletOn.Location = new System.Drawing.Point(6, 20);\r
+            this.checkBoxPushbulletOn.Name = "checkBoxPushbulletOn";\r
+            this.checkBoxPushbulletOn.Size = new System.Drawing.Size(48, 16);\r
+            this.checkBoxPushbulletOn.TabIndex = 0;\r
+            this.checkBoxPushbulletOn.Text = "有効";\r
+            this.checkBoxPushbulletOn.UseVisualStyleBackColor = true;\r
+            // \r
             // groupBoxKancolleDb\r
             // \r
             this.groupBoxKancolleDb.Controls.Add(this.textBoxKancolleDbToken);\r
@@ -836,15 +893,15 @@ namespace KancolleSniffer
             // \r
             // textBoxKancolleDbToken\r
             // \r
-            this.textBoxKancolleDbToken.Location = new System.Drawing.Point(135, 18);\r
+            this.textBoxKancolleDbToken.Location = new System.Drawing.Point(121, 18);\r
             this.textBoxKancolleDbToken.Name = "textBoxKancolleDbToken";\r
-            this.textBoxKancolleDbToken.Size = new System.Drawing.Size(93, 19);\r
+            this.textBoxKancolleDbToken.Size = new System.Drawing.Size(107, 19);\r
             this.textBoxKancolleDbToken.TabIndex = 2;\r
             // \r
             // label8\r
             // \r
             this.label8.AutoSize = true;\r
-            this.label8.Location = new System.Drawing.Point(73, 21);\r
+            this.label8.Location = new System.Drawing.Point(57, 21);\r
             this.label8.Name = "label8";\r
             this.label8.Size = new System.Drawing.Size(63, 12);\r
             this.label8.TabIndex = 1;\r
@@ -855,9 +912,9 @@ namespace KancolleSniffer
             this.checkBoxKancolleDbOn.AutoSize = true;\r
             this.checkBoxKancolleDbOn.Location = new System.Drawing.Point(6, 20);\r
             this.checkBoxKancolleDbOn.Name = "checkBoxKancolleDbOn";\r
-            this.checkBoxKancolleDbOn.Size = new System.Drawing.Size(67, 16);\r
+            this.checkBoxKancolleDbOn.Size = new System.Drawing.Size(48, 16);\r
             this.checkBoxKancolleDbOn.TabIndex = 0;\r
-            this.checkBoxKancolleDbOn.Text = "送信する";\r
+            this.checkBoxKancolleDbOn.Text = "有効";\r
             this.checkBoxKancolleDbOn.UseVisualStyleBackColor = true;\r
             // \r
             // tabPageDebug\r
@@ -1010,6 +1067,8 @@ namespace KancolleSniffer
             this.tabPageLog.PerformLayout();\r
             ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaterialLogInterval)).EndInit();\r
             this.tabPageCollabo.ResumeLayout(false);\r
+            this.groupBoxPushbullet.ResumeLayout(false);\r
+            this.groupBoxPushbullet.PerformLayout();\r
             this.groupBoxKancolleDb.ResumeLayout(false);\r
             this.groupBoxKancolleDb.PerformLayout();\r
             this.tabPageDebug.ResumeLayout(false);\r
@@ -1099,5 +1158,10 @@ namespace KancolleSniffer
         private System.Windows.Forms.Label label14;\r
         private System.Windows.Forms.Label label15;\r
         private System.Windows.Forms.Button buttonDetailedSettings;\r
+        private System.Windows.Forms.GroupBox groupBoxPushbullet;\r
+        private System.Windows.Forms.TextBox textBoxPushbulletToken;\r
+        private System.Windows.Forms.Label label7;\r
+        private System.Windows.Forms.CheckBox checkBoxPushbulletOn;\r
+        private System.Windows.Forms.Button buttonPushbulletTest;\r
     }\r
 }
\ No newline at end of file
index 24d15d7..d6ff9e0 100644 (file)
@@ -92,8 +92,11 @@ namespace KancolleSniffer
             LoadProxySettings();\r
             LoadLogSettings();\r
             LoadDebugSettings();\r
+\r
             checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
             textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
+            checkBoxPushbulletOn.Checked = _config.Pushbullet.On;\r
+            textBoxPushbulletToken.Text = _config.Pushbullet.Token;\r
         }\r
 \r
         private void LoadProxySettings()\r
@@ -158,8 +161,11 @@ namespace KancolleSniffer
                 DialogResult = DialogResult.None;\r
             ApplyLogSettings();\r
             ApplyDebugSettings();\r
+\r
             _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
             _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
+            _config.Pushbullet.On = checkBoxPushbulletOn.Checked;\r
+            _config.Pushbullet.Token = textBoxPushbulletToken.Text;\r
 \r
             _config.TopMost = checkBoxTopMost.Checked;\r
             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
@@ -339,5 +345,17 @@ namespace KancolleSniffer
         {\r
             _prevPosition = Location;\r
         }\r
+\r
+        private void buttonPushbulletTest_Click(object sender, EventArgs e)\r
+        {\r
+            try\r
+            {\r
+                PushBullet.PushNote(textBoxPushbulletToken.Text, "KancolleSniffer", "うまくいったかな?");\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(this, ex.Message, "Pushbulletエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
+        }\r
     }\r
 }
\ No newline at end of file
index b1bc4fc..3cd6901 100644 (file)
@@ -98,6 +98,7 @@
       <DependentUpon>NotificationConfigDialog.cs</DependentUpon>\r
     </Compile>\r
     <Compile Include="PresetDeck.cs" />\r
+    <Compile Include="PushBullet.cs" />\r
     <Compile Include="RepairListForMain.cs">\r
       <SubType>Component</SubType>\r
     </Compile>\r
index 8c4d1c5..043ffaf 100644 (file)
@@ -846,14 +846,21 @@ namespace KancolleSniffer
             }\r
         }\r
 \r
-        private void Ring(string baloonTitle, string baloonMessage, string name)\r
+        private void Ring(string balloonTitle, string balloonMessage, string name)\r
         {\r
             if (_config.FlashWindow && (_config.Notifications[name] & NotificationType.FlashWindow) != 0)\r
                 Win32API.FlashWindow(Handle);\r
             if (_config.ShowBaloonTip && (_config.Notifications[name] & NotificationType.ShowBaloonTip) != 0)\r
-                notifyIconMain.ShowBalloonTip(20000, baloonTitle, baloonMessage, ToolTipIcon.Info);\r
+                notifyIconMain.ShowBalloonTip(20000, balloonTitle, balloonMessage, ToolTipIcon.Info);\r
             if (_config.PlaySound && (_config.Notifications[name] & NotificationType.PlaySound) != 0)\r
                 PlaySound(_config.Sounds[name], _config.Sounds.Volume);\r
+            if (_config.Pushbullet.On && (_config.Notifications[name] & NotificationType.Pushbullet) != 0)\r
+            {\r
+                Task.Run(() =>\r
+                {\r
+                    PushBullet.PushNote(_config.Pushbullet.Token, balloonTitle, balloonMessage);\r
+                });\r
+            }\r
         }\r
 \r
         [DllImport("winmm.dll")]\r
index bc55c1d..02be4b8 100644 (file)
@@ -33,6 +33,7 @@
             this.checkBoxShowBaloonTip = new System.Windows.Forms.CheckBox();\r
             this.checkBoxPlaySound = new System.Windows.Forms.CheckBox();\r
             this.buttonClose = new System.Windows.Forms.Button();\r
+            this.checkBoxPushbullet = new System.Windows.Forms.CheckBox();\r
             this.SuspendLayout();\r
             // \r
             // listBoxNotifications\r
             // buttonClose\r
             // \r
             this.buttonClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;\r
-            this.buttonClose.Location = new System.Drawing.Point(141, 134);\r
+            this.buttonClose.Location = new System.Drawing.Point(141, 148);\r
             this.buttonClose.Name = "buttonClose";\r
             this.buttonClose.Size = new System.Drawing.Size(75, 23);\r
             this.buttonClose.TabIndex = 6;\r
             this.buttonClose.Text = "閉じる";\r
             this.buttonClose.UseVisualStyleBackColor = true;\r
             // \r
+            // checkBoxPushbullet\r
+            // \r
+            this.checkBoxPushbullet.AutoSize = true;\r
+            this.checkBoxPushbullet.Location = new System.Drawing.Point(12, 128);\r
+            this.checkBoxPushbullet.Name = "checkBoxPushbullet";\r
+            this.checkBoxPushbullet.Size = new System.Drawing.Size(77, 16);\r
+            this.checkBoxPushbullet.TabIndex = 7;\r
+            this.checkBoxPushbullet.Text = "Pushbullet";\r
+            this.checkBoxPushbullet.UseVisualStyleBackColor = true;\r
+            this.checkBoxPushbullet.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged);\r
+            // \r
             // NotificationConfigDialog\r
             // \r
             this.AcceptButton = this.buttonClose;\r
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);\r
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;\r
             this.CancelButton = this.buttonClose;\r
-            this.ClientSize = new System.Drawing.Size(228, 166);\r
+            this.ClientSize = new System.Drawing.Size(228, 183);\r
+            this.Controls.Add(this.checkBoxPushbullet);\r
             this.Controls.Add(this.buttonClose);\r
             this.Controls.Add(this.checkBoxPlaySound);\r
             this.Controls.Add(this.checkBoxShowBaloonTip);\r
         private System.Windows.Forms.CheckBox checkBoxShowBaloonTip;\r
         private System.Windows.Forms.CheckBox checkBoxPlaySound;\r
         private System.Windows.Forms.Button buttonClose;\r
+        private System.Windows.Forms.CheckBox checkBoxPushbullet;\r
     }\r
 }
\ No newline at end of file
index 95cda12..e4cc055 100644 (file)
@@ -18,6 +18,7 @@ namespace KancolleSniffer
             checkBoxFlashWindow.Tag = NotificationType.FlashWindow;\r
             checkBoxShowBaloonTip.Tag = NotificationType.ShowBaloonTip;\r
             checkBoxPlaySound.Tag = NotificationType.PlaySound;\r
+            checkBoxPushbullet.Tag = NotificationType.Pushbullet;\r
 \r
             // ReSharper disable once CoVariantArrayConversion\r
             listBoxNotifications.Items.AddRange(Config.NotificationNames);\r
@@ -31,6 +32,7 @@ namespace KancolleSniffer
             checkBoxFlashWindow.Checked = (notification & NotificationType.FlashWindow) != 0;\r
             checkBoxShowBaloonTip.Checked = (notification & NotificationType.ShowBaloonTip) != 0;\r
             checkBoxPlaySound.Checked = (notification & NotificationType.PlaySound) != 0;\r
+            checkBoxPushbullet.Checked = (notification & NotificationType.Pushbullet) != 0;\r
         }\r
 \r
         private void checkBox_CheckedChanged(object sender, EventArgs e)\r
diff --git a/KancolleSniffer/PushBullet.cs b/KancolleSniffer/PushBullet.cs
new file mode 100644 (file)
index 0000000..24411f9
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (C) 2017 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
+// \r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//    http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+using System.Net;\r
+using System.Text;\r
+\r
+namespace KancolleSniffer\r
+{\r
+    public class PushBullet\r
+    {\r
+        public static void PushNote(string token, string title, string body)\r
+        {\r
+            using (var wc = new WebClient())\r
+            {\r
+                wc.Headers[HttpRequestHeader.ContentType] = "application/json";\r
+                wc.Credentials = new NetworkCredential(token, "");\r
+                wc.Encoding = Encoding.UTF8;\r
+                wc.UploadString("https://api.pushbullet.com/v2/pushes",\r
+                    $"{{ \"type\": \"note\", \"title\": \"{title}\", \"body\": \"{body}\" }}");\r
+            }\r
+        }\r
+    }\r
+}
\ No newline at end of file