/
AppNotifications.pm
312 lines (243 loc) · 7.07 KB
/
AppNotifications.pm
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package Net::AppNotifications;
use strict;
use 5.008_001;
our $VERSION = '0.03';
use AnyEvent::HTTP;
use Carp;
use URI::Escape 'uri_escape_utf8';
use constant POST_URI =>
q{https://www.appnotifications.com/account/notifications.xml};
use constant KEY_URI =>
q{https://www.appnotifications.com/account/user_session.xml};
sub new {
my $class = shift;
my %param = @_;
my $notifier = bless { %param }, ref $class || $class;
unless ($param{key}) {
$param{key} = $class->get_key( %param );
}
croak "Key (or valid email, pass to get it) is needed" unless $param{key};
return $notifier;
}
sub send {
my $notifier = shift;
my %cbs;
my $key = $notifier->{key};
my $finish = sub {};
my %param;
if (scalar @_ == 1) {
my $message = shift;
unless (defined $message && length $message) {
croak "Please, give me a message to push";
}
$param{message} = $message;
my $done = AnyEvent->condvar;
$cbs{on_posted} = sub {
my ($data, $hds) = @_;
$done->send;
croak "Something happend" unless defined $data;
};
$cbs{on_timeout} = sub {
$done->send;
croak "timeout";
};
$cbs{on_error} = sub {
$done->send;
croak "Error $_[0]";
};
$finish = sub {
$done->recv;
};
}
else {
%param = @_;
$cbs{$_} = $param{$_} for qw{on_error on_timeout};
my $early_error = $cbs{on_error} || sub { croak "$_[0]" };
my $on_success = $param{on_success};
unless ($on_success) {
$early_error->("On success must be passed");
return;
}
## callback definitions
$cbs{on_posted} = sub {
my $data = shift;
unless (defined $data) {
$cbs{on_error}->("Something happened");
return;
}
$on_success->($data, @_);
};
$cbs{on_error} ||= sub {
warn "Error: $_[0]";
};
$cbs{on_timeout} ||= sub {
warn "Timeout: $_[0]";
};
}
my $notification_params = $notifier->normalize(%param);
my $uri = POST_URI;
my $body = build_body(
"user_credentials" => $key,
%$notification_params,
);
$notifier->post_request($uri, $body, %cbs);
## wait here for synchronous calls
$finish->();
return;
}
sub normalize {
my $notifier = shift;
my %param = @_;
my %nparam;
my $N = "notification";
my @keys = qw/
message message_level action_loc_key run_command
title long_message long_message_preview
icon_url subtitle
/;
for (@keys) {
next unless exists $param{$_};
$nparam{"${N}[$_]"} = $param{$_} || "";
}
my $silent = 0;
my $sound = $param{sound};
if ($sound) {
unless ($sound =~ /^[1..7]$/) {
$sound = "1";
}
$sound .= ".caf";
}
if (my $s = $param{silent}) {
$s = lc $s;
unless ($s eq 'off' or $s eq 'no' or $s eq 'false') {
$silent = 1;
$sound = undef;
}
}
$nparam{"${N}[silent]"} = $silent;
$nparam{"${N}[sound]"} = $sound || "";
return \%nparam;
}
sub post_request {
my $notifier = shift;
my ($uri, $body, %cbs) = @_;
http_request
POST => $uri,
body => $body,
headers => {
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => q{yann's Net::AppNotifications},
},
on_header => sub {
my ($hds) = @_;
if ($hds->{Status} ne '200') {
return $cbs{on_error}->("$hds->{Status}: $hds->{Reason}");
}
return 1;
},
$cbs{on_posted};
return
}
sub get_key {
my $class = shift;
my $done = AnyEvent->condvar;
my $key;
my $got_key = sub { $key = shift; $done->send };
$class->async_get_key( @_, got_key => $got_key );
$done->recv;
return $key;
}
sub async_get_key {
my $class = shift;
my %param = shift;
my $email = $param{email} or return;
my $password = $param{password};
my $uri = KEY_URI;
my $body = build_body(
'user_session[email]' => $email,
'user_session[password]' => $password,
);
my %cbs = (
on_posted => sub {
my $key = shift;
$param{got_key}->($key);
},
);
$class->post_request($uri, $body, %cbs);
return;
}
sub build_body {
my %param = @_;
return
join "&", map {
join "=", $_, uri_escape_utf8($param{$_})
}
keys %param;
}
1;
__END__
=encoding utf-8
=for stopwords
=head1 NAME
Net::AppNotifications - send notifications to your iPhone.
=head1 SYNOPSIS
$notifier = Net::AppNotifications->new(
email => $registered_email,
password => $password,
);
# - or, prefered -
$notifier = Net::AppNotifications->new(
key => $key,
);
## Synchronous blocking notification
if ($notifier->send("Hello, Mr Jobs")) {
print "Notification delivered";
}
## Asynchronous non-blocking notification
my $sent = AnyEvent->condvar;
my $handle = $notifier->send(
message => "Hello!", # shows up in the notification
long_message => "<b>html allowed</b>", # in the iPhone app
long_message_preview => "the notif preview", # in the iPhone app
title => "the notification title", # in the iPhone app
sound => 2, # override default audible bell
silent => 0, # if true, make sure there is no bell
message_level => -2, # priority ([-2, 2])
action_loc_key => "Approve me", # button on the notification
# what happened when clicked
run_command => "http://maps.google.com/maps?q=cupertino",
## delivery callbacks
on_error => $error_cb,
on_timeout => $timeout_cb,
on_success => sub { $sent->send },
);
$sent->recv;
## returns undef in case of error
$key = Net::AppNotifications->get_key(
email => $registered_email,
password => $password,
);
=head1 DESCRIPTION
Net::AppNotifications is a wrapper around appnotifications.com. It allows
you to push notifications to your iPhone(s) registered with the service.
A visual and audible alert (like for SMS) will arrive on your device
in a limited timeframe.
appnotifications allows you to tweak different aspect of the notification
received:
=over 4
=item * the message
=item * the sound played, if any
=item * the title of the accept button of the notification
=item * what happens once accepted
=item * ... more, see the L<SYNOPSIS>
=back
If you already have an APNS key, I recommend using L<AnyEvent::APNS>,
directly.
=head1 AUTHOR
Yann Kerherve E<lt>yannk@cpan.orgE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<AnyEvent::APNS>, L<http://www.appnotifications.com>
=cut