APNS-Agent

 view release on metacpan or  search on metacpan

lib/APNS/Agent.pm  view on Meta::CPAN

            else {
                delete $self->{_send_timer};
            }
        },
    );
}

sub _send {
    my ($self, $token, $payload) = @_;

    local $@;
    my $identifier;
    eval {
        $identifier = $self->_apns->send(pack("H*", $token) => {
            aps => $payload,
        });
    };

    if (my $err = $@) {
        if ($err =~ m!Can't call method "push_write" on an undefined value!) {
            # AnyEvent::APNS->handle is missing
            delete $self->{_send_timer};
            unshift @{ $self->_queue }, [$token, $payload];
            $self->_connect_to_apns;
        }
        else {
            die $err;
        }
    }
    else {
        $self->_sent_cache->set($identifier => {
            token   => $token,
            payload => $payload,
        });
        $self->_last_sent_at(time);
        infof "event:send\ttoken:$token\tidentifier:$identifier";
        $self->{_sent}++;
        $identifier;
    }
}

sub parse_options {
    my ($class, @argv) = @_;

    require Getopt::Long;
    require Pod::Usage;
    require Hash::Rename;

    my $p = Getopt::Long::Parser->new(
        config => [qw/posix_default no_ignore_case auto_help pass_through bundling/]
    );
    $p->getoptionsfromarray(\@argv, \my %opt, qw/
        certificate=s
        private-key=s
        disconnect-interval=i
        sandbox!
        debug-port=i
    /) or Pod::Usage::pod2usage();
    Pod::Usage::pod2usage() if !$opt{certificate} || !$opt{'private-key'};

    Hash::Rename::hash_rename(\%opt, code => sub {tr/-/_/});
    (\%opt, \@argv);
}

sub run {
    my $self = shift;
    my %args = @_ == 1 ? %{$_[0]} : @_;
    if (!$args{listen} && !$args{port} && !$ENV{SERVER_STARTER_PORT}) {
        $args{port} = 4905;
    }
    require Plack::Loader;
    Plack::Loader->load(Twiggy => %args)->run($self->to_app);
}

1;
__END__

=encoding utf-8

=head1 NAME

APNS::Agent - agent server for APNS

=head1 SYNOPSIS

    use APNS::Agent;
    my $agent = APNS::Agent->new(
        certificate => '/path/to/certificate',
        private_key => '/path/to/private_key',
    );
    $agent->run;

=head1 DESCRIPTION

APNS::Agent is agent server for APNS. It is also backend class of L<apns-agent>.

This module provides consistent connection to APNS and cares reconnection. It utilizes
L<AnyEvent::APNS> internally.

B<THE SOFTWARE IS ALPHA QUALITY. API MAY CHANGE WITHOUT NOTICE.>

=head1 API PARAMETERS

APNS::Agent launches HTTP Server process which accepts only POST method and
C<application/x-www-form-urlencoded> format parameters.

Acceptable parameters as follows:

=over

=item C<token>

device token by HEX format. (Required)

=item C<payload>

JSON string for push notification. If you only want to send message, alternatively can use
C<alert> parameter.

One of C<payload> and C<alert> must be supplied. Both of C<payload> and C<alert> are specified,
the C<payload> parameter has priority.



( run in 2.631 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )