AnyEvent-APNS
view release on metacpan or search on metacpan
0.10 2012-11-28T09:22:31+09:00
- use enhanced notification format (mash)
- added last_identifier, on_error_response property (mash)
0.09 2012-08-28T15:54:19+09:00
- Fixed read error wasn't occurred correctly because didn't start read event watcher.
- Added optional on_eof handler.
0.08 2012-08-27T18:27:56+09:00
- Added an option that accepts certificate and private_key datas directly instead of its file path. (shin1rosei)
0.07 2010-02-16T14:08:32+09:00
- fix reconnect issue: ->connect warn 'already connected' but sometimes actially already be disconnected
0.06 2009-10-15T17:19:13+09:00
- cast badge number to int value explicitly
- fix a bug trimming wring target... s/$payload->{alert}/$payload->{aps}{alert}/.
0.05 2009-10-07T12:54:58+09:00
- now send method trimmed payload body if its length over 256bytes;
lib/AnyEvent/APNS.pm view on Meta::CPAN
use JSON::Any;
our $VERSION = '0.10';
has certificate => (
is => 'rw',
isa => 'Str | ScalarRef',
required => 1,
);
has private_key => (
is => 'rw',
isa => 'Str | ScalarRef',
required => 1,
);
has sandbox => (
is => 'rw',
isa => 'Bool',
default => 0,
);
lib/AnyEvent/APNS.pm view on Meta::CPAN
or return $self->on_error->(undef, 1, $!);
my $tls_setting = {};
if (ref $self->certificate) {
$tls_setting->{cert} = ${ $self->certificate };
}
else {
$tls_setting->{cert_file} = $self->certificate;
}
if (ref $self->private_key) {
$tls_setting->{key} = ${ $self->private_key };
}
else {
$tls_setting->{key_file} = $self->private_key;
}
my $handle = AnyEvent::Handle->new(
fh => $fh,
on_error => sub {
$self->on_error->(@_);
$self->clear_handler;
$_[0]->destroy;
},
!$self->is_debug ? (
lib/AnyEvent/APNS.pm view on Meta::CPAN
AnyEvent::APNS - Simple wrapper for Apple Push Notifications Service (APNS) provider
=head1 SYNOPSIS
use AnyEvent::APNS;
my $cv = AnyEvent->condvar;
my $apns; $apns = AnyEvent::APNS->new(
certificate => 'your apns certificate file',
private_key => 'your apns private key file',
sandbox => 1,
on_error => sub { # something went wrong },
on_connect => sub {
my $identifier = $apns->send( $device_token => {
aps => {
alert => 'Message received from Bob',
},
});
},
on_error_response => sub {
lib/AnyEvent/APNS.pm view on Meta::CPAN
You should call connect method explicitly to connect server.
=head1 METHODS
=head2 new
Create APNS object.
my $apns = AnyEvent::APNS->new(
certificate => 'your apns certificate file',
private_key => 'your apns private key file',
sandbox => 1,
on_error => sub { # something went wrong },
);
Supported arguments are:
=over 4
=item certificate => 'Str | ScalarRef'
certificate => '/path/to/certificate_file',
# or
certificate => \$certificate,
Required. Either file path for certificate or scalar-ref of certificate data.
=item private_key => 'Str | ScalarRef'
private_key => '/path/to/private_key',
# or
private_key => \$private_key,
Required. Either file path for private_key or scalar-ref of private-key data.
=item sandbox => 0|1
This is a flag indicate target service is provisioning (sandbox => 1) or distribution (sandbox => 0)
Optional (Default: 0)
=item on_error => $cb->($handle, $fatal, $message)
Callback to be called when something error occurs.
t/01_simple.t view on Meta::CPAN
plan tests => 9;
use AnyEvent::APNS;
use AnyEvent::Socket;
my $port = empty_port;
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => 'dummy',
private_key => 'dummy',
on_error => sub { die $! },
on_connect => sub {
my $identifier = $apns->send('d' x 32 => { foo => 'bar' });
is( $identifier, 1, '1st identifier is 1' );
},
);
my $cv = AnyEvent->condvar;
# test server
t/02_trim.t view on Meta::CPAN
my $payloads = [
{ aps => { alert => 'ããã«ã¡ã¯'x100, } },
{ aps => { alert => { body => 'ããã«ã¡ã¯'x100, } } },
];
for my $payload (@$payloads) {
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => 'dummy',
private_key => 'dummy',
on_error => sub { die $! },
on_connect => sub {
$apns->send('d' x 32 => $payload);
},
);
my $cv = AnyEvent->condvar;
# test server
my $connect_state = 'initial';
t/03_validate.t view on Meta::CPAN
use Test::TCP;
plan tests => 4;
my $port = empty_port;
lives_ok {
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => 'dummy',
private_key => 'dummy',
);
} 'set certificate and private_key ok';
lives_ok {
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => \'dummy',
private_key => \'dummy',
);
} 'set certificate ref and private_key ref ok';
throws_ok {
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
private_key => 'dummy',
);
} qr/certificate.+is required/
, 'not set certificate';
throws_ok {
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => 'dummy',
);
} qr/private_key.+is required/
, 'not set private_key';
# close immediately
close $fh;
};
# without on_eof
$cv = AnyEvent->condvar;
my $apns; $apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => \'',
private_key => \'',
on_error => sub {
my ($h, $fatal, $msg) = @_;
like $msg, qr/^Unexpected end-of-file/, 'eof ok';
$cv->send;
},
);
$apns->connect;
$cv->recv;
# on_eof
$cv = AnyEvent->condvar;
$apns = AnyEvent::APNS->new(
debug_port => $port,
certificate => \'',
private_key => \'',
on_error => sub {
my ($h, $fatal, $msg) = @_;
fail 'on_eof not called: ' . $msg;
$cv->send;
},
on_eof => sub {
my ($h) = @_;
pass 'on_eof called ok';
$cv->send;
},
my $cer = "$ENV{HOME}/dev/apns/test.cer";
my $key = "$ENV{HOME}/dev/apns/test.key";
my $token = file("$ENV{HOME}/dev/apns/token.bin")->slurp;
{
my $cv = AnyEvent->condvar;
my $apns; $apns = AnyEvent::APNS->new(
certificate => $cer,
private_key => $key,
sandbox => 1,
on_connect => sub {
$apns->send($token => { aps => { alert => "ãã¹ãï¼" }});
$apns->handler->on_drain(sub { undef $_[0]; $cv->send });
},
)->connect;
$cv->recv;
ok(1, "app runs ok, check your phone");
{
my $cer_content = file($cer)->slurp;
my $key_content = file($key)->slurp;
my $cv = AnyEvent->condvar;
my $apns; $apns = AnyEvent::APNS->new(
certificate => \$cer_content,
private_key => \$key_content,
sandbox => 1,
on_connect => sub {
$apns->send($token => { aps => { alert => "ããä¸åãã¹ãï¼" }});
$apns->handler->on_drain(sub { undef $_[0]; $cv->send });
},
)->connect;
$cv->recv;
ok(1, "app runs ok, check your phone again");
( run in 0.486 second using v1.01-cache-2.11-cpan-a5abf4f5562 )