line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::View::APNS; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
2402
|
use Net::APNS; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use base qw( Catalyst::View ); |
6
|
|
|
|
|
|
|
use Data::Dumper; |
7
|
|
|
|
|
|
|
use Carp; |
8
|
|
|
|
|
|
|
use Catalyst::Exception; |
9
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(apns cv certification private_key passwd)); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
my ( $class, $c, $arguments ) = @_; |
15
|
|
|
|
|
|
|
my $self = $class->next::method($c); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
for my $field ( keys(%$arguments) ) { |
18
|
|
|
|
|
|
|
next unless $field; |
19
|
|
|
|
|
|
|
next if $field ne 'apns'; |
20
|
|
|
|
|
|
|
my $subs = $arguments->{$field}; |
21
|
|
|
|
|
|
|
for my $subfield ( keys(%$subs) ) { |
22
|
|
|
|
|
|
|
if ( $self->can($subfield) ) { |
23
|
|
|
|
|
|
|
$self->$subfield( $subs->{$subfield} ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else { |
26
|
|
|
|
|
|
|
$c->log->debug( "Invalied parameter " . $subfield ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
unless ( $self->certification ) { |
31
|
|
|
|
|
|
|
croak "Invalied certification"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
unless ( $self->private_key ) { |
34
|
|
|
|
|
|
|
croak "Invalied private_key"; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
return $self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub process { |
40
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
41
|
|
|
|
|
|
|
my $apns = Net::APNS->new; |
42
|
|
|
|
|
|
|
my $notify = $apns->notify( |
43
|
|
|
|
|
|
|
{ |
44
|
|
|
|
|
|
|
cert => $self->certification, |
45
|
|
|
|
|
|
|
key => $self->private_key, |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
if ( $self->passwd ) { |
49
|
|
|
|
|
|
|
$notify->passwd( $self->passwd ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
$notify->sandbox( $c->stash->{apns}->{sandbox} ) if $c->stash->{apns}->{sandbox}; |
52
|
|
|
|
|
|
|
unless ( $c->stash->{apns}->{device_token} ) { |
53
|
|
|
|
|
|
|
croak "Invalied device token"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
$notify->devicetoken( $c->stash->{apns}->{device_token} ); |
56
|
|
|
|
|
|
|
$notify->message( $c->stash->{apns}->{message} ) |
57
|
|
|
|
|
|
|
if $c->stash->{apns}->{badge}; |
58
|
|
|
|
|
|
|
$notify->badge( $c->stash->{apns}->{badge} ) if $c->stash->{apns}->{badge}; |
59
|
|
|
|
|
|
|
$notify->write; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
__END__ |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Catalyst::View::APNS - APNS View Class. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# lib/MyApp/View/APNS.pm |
72
|
|
|
|
|
|
|
package MyApp::View::APNS; |
73
|
|
|
|
|
|
|
use base qw/Catalyst::View::APNS/; |
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Configure in lib/MyApp.pm |
77
|
|
|
|
|
|
|
MyApp->config( |
78
|
|
|
|
|
|
|
{ |
79
|
|
|
|
|
|
|
apns => { |
80
|
|
|
|
|
|
|
certification => cert #require to specify |
81
|
|
|
|
|
|
|
private_key => key #require to specify |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub hello : Local { |
87
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
88
|
|
|
|
|
|
|
$c->stash->{apns} = { |
89
|
|
|
|
|
|
|
device_token => $device_token, |
90
|
|
|
|
|
|
|
message => $message, |
91
|
|
|
|
|
|
|
badge => $badge, |
92
|
|
|
|
|
|
|
sandbox => 0 | 1 #optional |
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
$c->forward('MyApp::View::APNS'); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Use the helper to create your View: |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
myapp_create.pl view APNS APNS |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 DESCRIPTION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Catalyst::View::APNS is Catalyst view handler that Apple Push Notification Service. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 AUTHOR |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
haoyayoi E<lt>st.hao.yayoi@gmail.comE<gt> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 SEE ALSO |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
L<Net::APNS> |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 LICENSE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
116
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |