俊采星驰 2017-10-18
Net::APNS is Apple Push Notification Service. Push message to iPhone and get unavalble-devicetoken.
苹果开发者官网说明:The APNs provider API lets you send remote notification requests to APNs.
Net::APNS模块已经封装了整个推送逻辑,调用者只需提供设备的deviceToken和要推送的消息即可。
举例如下:
use Net::APNS;
sub net_apns {
my $devicetoken = $_[0];
my $content = $_[1];
return 0 if($devicetoken eq "");
return 0 if($content eq "");
my $message; #用户自定义的内容
my $message->{obj} = "apple";
$message->{act} = "push";
$content = encode( "utf8", $content );
$message->{content} = $content;
my $APNS = Net::APNS->new;
# 提供相关证书,证书生成可参考http://help.apple.com/xcode/mac/current/#/dev11b059073
# 或者参考: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"); # 推送内容
$Notifier->badge(1); #ios手机里面的app右上角会出现红点且数字为1
if(__PACKAGE__ eq "PRODUCT") {
$Notifier->sandbox(0); # 是否使用沙盒测试环境, 0表示“否”
} else {
$Notifier->sandbox(1); # 1表示“是”
}
$Notifier->sound('default');
$Notifier->custom($message); #用户自定义的内容
# $Notifier->custom({custom_key =>'i am custom_value'});
my $result_code = $Notifier->write;
if ($result_code) {
return "send success";
} else {
return "send failed";
}
}
因为App的功能需要,最近一直在调研苹果的APNs推送,开始时觉得超麻烦,现在感觉还是比较easy,“难者不会,会者不难”,自己踩过了这么多的坑终于会了,不出来吐槽(装X)一下对不起自己,23333。