-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushbullet.m
78 lines (63 loc) · 2.3 KB
/
pushbullet.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
%==========================================================================
% FUNCTION SENDS A PUSHBULLET NOTE
% ~ https://github.com/willbowditch/matlab-pushbullet
%==========================================================================
% - find your API key (AKA Access token)at:
% https://www.pushbullet.com/account and set below.
% - use pushbullet('title', 'message') to send push notifications.
% - Input must be string, so use sprintf to convert variables
% beforehand.
% - optional, can also provide api key if you'd rather leave it out of the
% function (pushbullet('title','message','apikey'))
%==========================================================================
function pushbullet (title, message, apikey)
try
if ~exist('message','var')
message='';
end
if ~exist('apikey', 'var')
% SET YOUR API KEY HERE, IF YOU DON'T WANT TO INPUT IT EVERY TIME
apikey='';
end
host='https://api.pushbullet.com/v2/pushes';
params={'type' 'note' 'title' title 'body' message};
%Convert to a URL post String
str = '';
for i=1:2:length(params)
if (i == 1), separator = '';
else separator = '&';
end
param = urlencode(params{i});
value = urlencode(params{i+1});
str = [str separator param '=' value];
end
assert(usejava('jvm'),'Function requires Java')
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings
%Create a urlConnection
handler = sun.net.www.protocol.https.Handler;
url = java.net.URL([],host,handler);
urlConnection = url.openConnection;
urlConnection.setRequestMethod('POST');
urlConnection.setFollowRedirects(true);
urlConnection.setReadTimeout(0);
%Authorise Connection
encoder = sun.misc.BASE64Encoder();
out = char(encoder.encode(java.lang.String([apikey ':' '']).getBytes()));
urlConnection.setRequestProperty('Authorization', ['Basic ' out]);
%Prepare Data
body = unicode2native(str,'');
%Send!
urlConnection.setRequestProperty('Content-Length',int2str(length(body)));
urlConnection.setDoOutput(true);
outputStream = urlConnection.getOutputStream;
outputStream.write(body);
outputStream.close;
inputStream = urlConnection.getInputStream;
%Finsh up
inputStream.close;
outputStream.close;
urlConnection.disconnect;
catch err
disp('Warning! Push notification failed... wrong api key? No internet?');
end