Perl's Net::APNS implements Apple message push

Net::APNS is Apple Push Notification Service. Push message to iPhone and get unavalble-devicetoken.

 

Apple developer official website description: The APNs provider API lets you send remote notification requests to APNs. 

 

The Net::APNS module has encapsulated the entire push logic, and the caller only needs to provide the deviceToken of the device and the message to be pushed.

An example is as follows:

use Net::APNS;

 

sub net_apns {

     my $devicetoken = $_[0];

     my $content = $_[1];

     return 0 if($devicetoken eq "");

     return 0 if($content eq "");

 

     my $message; #User-defined content

     my $message->{obj} = "apple";

     $message->{act} = "push";

 

     $content =  encode( "utf8", $content );

     $message->{content} = $content;

 

     my $APNS = Net::APNS->new; 

     # Provide relevant certificates, please refer to http://help.apple.com/xcode/mac/current/#/dev11b059073 for certificate generation

     # Or reference: http://blog.sina.com.cn/s/blog_4adf31ea010175wo.html

     my $Notifier = $APNS->notify({

          cert => "/var/www/apple/pushck.pem",

          key => "/var/www/apple/PushChatkey.pem",

          passwd => "123456"

     });

 

     $Notifier->devicetoken("$devicetoken"); 

     $Notifier->message("$content"); # push content

     $Notifier->badge(1); # There will be a red dot in the upper right corner of the app in the ios phone and the number is 1

     if(__PACKAGE__ eq "PRODUCT") { 

          $Notifier->sandbox(0);   # Whether to use the sandbox test environment, 0 means " no "

    } else {

          $Notifier->sandbox(1); # 1 means " yes "

     }

     $Notifier->sound('default'); 

     $Notifier->custom($message); #User-defined content

     # $Notifier->custom({custom_key =>'i am custom_value'});

     my $result_code = $Notifier->write;

     if ($result_code) {

          return "send success";

     } else {

          return "send failed";

     } 

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326162944&siteId=291194637