javapns 推送ios消息

javapns 推送消息过程:(我这边是PC发送)

  1. 客户端(即PC)发送一个请求
  2. 我们自己的服务端接收请求
  3. 服务端通过apns,往苹果服务器发送消息推送请求
  4. 苹果服务器收到请求,根据设备devicetoken,然后往设备发送消息,并返回结果给我们自己的服务端
  5. 我们服务端返回结果给客户端

需要jar包,我这有现成的
javapns jar 下载
jar包maven地址:

<dependency>
    <groupId>com.github.fernandospr</groupId>
    <artifactId>javapns-jdk16</artifactId>
    <version>2.3.1</version>
</dependency>

下面请看代码

package pushmessage;

import java.util.ArrayList;
import java.util.List;

import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;

public class PushMsg {
    public static void main(String[] args) throws Exception{

            System.out.println("zsl==========开始推送消息");
            int badge = 1; // 图标小红圈的数值
            String sound = "default"; // 铃音
            String msgCertificatePassword = "12345678";//导出证书时设置的密码
            String deviceToken = "6846f471b699868036547869977935cbf98df3805ecb2c1b7e7140b4fc962c94"; //手机设备token号
            String message = "test push message to ios device";

            List<String> tokens = new ArrayList<String>();
            tokens.add(deviceToken);

//          String certificatePath = requestRealPath
//                  + "/WEB-INF/classes/certificate/msg.p12";
            //java必须要用导出p12文件  php的话是pem文件
            String certificatePath = "F:/mdm.p12";
            boolean sendCount = true;

            PushNotificationPayload payload = new PushNotificationPayload();
            payload.addAlert(message); // 消息内容
            payload.addBadge(badge);


            //payload.addCustomAlertBody(msgEX);
            if (null == sound || "".equals(sound)) {
                payload.addSound(sound);
            }

            PushNotificationManager pushManager = new PushNotificationManager();
            // true:表示的是产品测试推送服务 false:表示的是产品发布推送服务
            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
                    certificatePath, msgCertificatePassword, true));
            List<PushedNotification> notifications = new ArrayList<PushedNotification>();
            // 开始推送消息
            if (sendCount) {
                Device device = new BasicDevice();
                device.setToken(deviceToken);
                PushedNotification notification = pushManager.sendNotification(
                        device, payload, true);
                notifications.add(notification);
            } else {
                List<Device> devices = new ArrayList<Device>();
                for (String token : tokens) {
                    devices.add(new BasicDevice(token));
                }
                notifications = pushManager.sendNotifications(payload, devices);
            }

            List<PushedNotification> failedNotification = PushedNotification
                    .findFailedNotifications(notifications);
            List<PushedNotification> successfulNotification = PushedNotification
                    .findSuccessfulNotifications(notifications);
            int failed = failedNotification.size();
            int successful = successfulNotification.size();
            System.out.println("zsl==========成功数:" + successful);
            System.out.println("zsl==========失败数:" + failed);
            pushManager.stopConnection();
            System.out.println("zsl==========消息推送完毕");
    }
}

直接运行main函数就ok,测试通过。

注意:该jar包依赖于 log4j.jar 所以你的项目里还应该有log4j的jar包,不然会报错,log4j jar 包不要错了。

log4j maven地址:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.11</version>
</dependency>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌谚轩

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值