Perl之Net::APNS实现苹果消息推送
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";
}
}
上一篇: Seam和Quartz集成
下一篇: XML中的代码注释书写方法的详解