iOS 设备通过 java-apns 组件实现苹果 APNs 消息推送实现

导读:APNS(英文全称:Apple Push Notification Service),中文翻译为:苹果推送通知服务。该技术由苹果公司提供的APNS服务。

1、java-apns 组件需要的jar
https://github.com/downloads/notnoop/java-apns/apns-0.1.5-jar-with-dependencies.jar

note:which includes all the dependencies

2、将jar文件加入到maven仓库
mvn install:install-file -DgroupId=apns -DartifactId=apns -Dversion=0.1.5 -Dpackaging=jar -Dfile=./apns-0.1.5-jar-with-dependencies.jar

3、在代码中引入依赖文件
Java APNS project is available through Central Maven repository. To use java-apns in your project, you can simply add the following to your pom.xml file:

<dependency>
       <groupId>apns</groupId>
       <artifactId>apns</artifactId>
       <version>0.1.5</version>
</dependency>

4、java代码实现
 
   /**
     * iOS 设备通过 java-apns 组件实现苹果 APNs 消息推送实现
     * @param args
     */
    public static void main(String[] args){
        
        /**APNS推送需要的证书、密码、和设备的Token**/
        String  p12Path = "D:/MbaikeDevCertificates.p12";
        String  password = "123456";
        String  pushToken = "b868031f 54f87b60 a391824b 4e75d16e a45d50ab ca47ecb1 08660bae ab87b83b";
        
        try {
            /**设置参数,发送数据**/
            ApnsService service =APNS.newService().withCert(p12Path,password).withSandboxDestination().build();
            String payload = APNS.newPayload().alertBody("hello,www.mbaike.net").badge(1).sound("default").build();
            service.push(pushToken, payload);
            System.out.println("推送信息已发送!");
        } catch (Exception e) {
            System.out.println("出错了:"+e.getMessage());
        }
    }

你可能感兴趣的:(消息推送,apns,移动互联百科)