AnyEvent-APNS
view release on metacpan or search on metacpan
lib/AnyEvent/APNS.pm view on Meta::CPAN
# disconnect and exit program as soon as possible after sending a message
# otherwise $apns makes persistent connection with apns server
$apns->handler->on_drain(sub {
undef $_[0];
$cv->send;
});
$cv->recv;
=head1 DESCRIPTION
This module helps you to create Apple Push Notifications Service (APNS) Provider.
=head1 NOTE FOR 0.01x USERS
From 0.02, this module does not connect in constructor.
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.
This is wrapper for L<AnyEvent::Handle>'s on_error callbacks. Look at the document for more detail.
Optional (Default: just warn error)
=item on_eof => $cb->($handle)
Callback to be called when an end-of-file condition is detected.
Optional. (Default: use on_error instead. read L<AnyEvent::Handle> for more detail)
=item on_connect => $cb->()
Callback to be called when connection established to apns server.
Optional (Default: empty coderef)
=item on_error_response => $cb->($identifier, $status)
Callback to be called when APNs detects notification malformed or otherwise unintelligible.
C<$status> codes are documented here: L<http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html>
C<$identifier> is the return value of C<send>.
Optional (Default: ignore)
=back
=head2 $apns->connect;
Connect to apns server.
=head2 $apns->send( $device_token, \%payload )
Send apns messages with C<\%payload> to device specified C<$device_token>.
my $identifier = $apns->send( $device_token => {
aps => {
alert => 'Message received from Bob',
},
});
C<$device_token> should be a binary 32bytes device token provided by iPhone SDK (3.0 or above)
C<\%payload> should be a hashref suitable to apple document: L<http://developer.apple.com/iPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html>
Note: If you involve multi-byte strings in C<\%payload>, it should be utf8 decoded strings not utf8 bytes.
Store C<$identifier> with your C<$device_token> to react to C<on_error_response>.
=head2 $apns->handler
Return L<AnyEvent::Handle> object which is used to current established connection. It returns undef before connection completed.
=head1 TODO
=over 4
=item *
More correct error handling
( run in 0.892 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )