Pushbullet APIをC#で使うには。

主な用途としては、WindowsのプログラムでC#を使っていてスマフォなどに通知を送らなければならない様な時。

他のcsからPush("title","body")で使える。手軽で良い。

using Pushbullet_plugins;

       Push("title","body");

        private void Push(string title, string body)
        {
            Push Test = new Push();
            Test.AccessToken = ""; https://www.pushbullet.com/accountにある
            Test.DeviceToken = ""; GetDeviceList()使って取得する。

            //UrlEncodeでエンコードしないと文字化けする。

            Test.SendNote(System.Web.HttpUtility.UrlEncode(title), System.Web.HttpUtility.UrlEncode(body), Test.DeviceToken, Test.AccessToken);

        }

Push.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Codeplex.Data;
using System.Web;
using System.Net;
using System.Threading.Tasks;
using Codeplex.Data; //DynamicJSONの為

namespace Pushbullet_plugins
{
    public class Push
    {
        public string AccessToken { get; set; }
        public string DeviceToken { get; set; }

        public WebClient AuthenticatedWebClient(string Access_Token)// https://www.pushbullet.com/accountにある
        {
            Access_Token = AccessToken;
            WebClient wc = new WebClient();
            wc.Proxy = null;
            string authEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(Access_Token + ":"));
            wc.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", authEncoded);

            return wc;
        }

	//Pushbulletに登録されているデバイスの取得
        public List<string> GetDeviceList()
        {

            System.Net.HttpWebRequest webreq = (System.Net.HttpWebRequest)
                    System.Net.WebRequest.Create("https://api.pushbullet.com/v2/devices");

            //認証の設定
            webreq.Credentials =
                new System.Net.NetworkCredential(AccessToken, "");

            //HttpWebResponseの取得
            System.Net.HttpWebResponse webres =
                (System.Net.HttpWebResponse)webreq.GetResponse();

            //受信して表示
            System.IO.Stream st = webres.GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(st);
            
            var json = DynamicJson.Parse(sr.ReadToEnd());
            var inputJson = json.devices;
            List<string> lst = new List<string>();
            lst.Clear();
            foreach (var item in inputJson)
            {
                //
                if (item.IsDefined("nickname") == true)
                {
                    string t = item.nickname.ToString() + ":" + item.iden.ToString(); //+ "\r\n";改行コードはいらない
                    lst.Add(t);
                }
            }
            return lst;
        }

        public void SendNote(string Title, string Body, string Device_token, string Access_Token)
        {
            WebClient webreq = AuthenticatedWebClient(Access_Token);

            webreq.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            webreq.Headers[HttpRequestHeader.Accept] = "application/json";

            //認証の設定
            webreq.Credentials =
                new System.Net.NetworkCredential(Device_token, "");

            //First parameters to send
            string parameters = String.Format("device_id={0}&type={1}", Device_token, "note");
            parameters += String.Format("&title={0}&body={1}", Title, Body);

            string result = webreq.UploadString("https://api.pushbullet.com/" + "/v2/pushes", parameters);
        }
    }
}