NAME

AnyEvent::APNS - Simple wrapper for Apple Push Notifications Service (APNS) provider

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 {
        $apns->send( $device_token => {
            aps => {
                alert => 'Message received from Bob',
            },
        });
    },
);

# 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;

DESCRIPTION

This module helps you to create Apple Push Notifications Service (APNS) Provider.

METHODS

new

Create APNS object and connect to apns service.

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:

certificate => 'your apns certificate file'

Required

private_key => 'your apns private key file',

Required

sandbox => 0|1

This is a flag indicate target service is provisioning (sandbox => 1) or distribution (sandbox => 0)

Optional (Default: 0)

on_error => $cb->($handle, $fatal, $message)

Callback to be called when something error occurs. This is wrapper for AnyEvent::Handle's on_error callbacks. Look at the document for more detail.

Optional (Default: just warn error)

on_connect => $cb->()

Callback to be called when connection established to apns server.

Optional (Default: empty coderef)

$apns->send( $device_token, \%payload )

Send apns messages with \%payload to device speficied $device_token.

$apns->send( $device_token => {
    aps => {
        alert => 'Message received from Bob',
    },
});

$device_token shuould be a binary 32bytes device token provided by iPhone SDK (3.0 or above)

\%payload should be a hashref suitable to apple document: http://developer.apple.com/iPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html

Note: If you involve multi-byte strings in \%payload, it should be utf8 decoded strings not utf8 bytes.

$apns->handler

Return AnyEvent::Handle object which is used to current established connection. It returns undef before connection completed.

TODO

  • More correct error handling

  • Auto recconection

AUTHOR

Daisuke Murase <typester@cpan.org>

COPYRIGHT AND LICENSE

Copyright (c) 2009 by KAYAC Inc.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.