java 实现 ios 推送_ios 推送 java 实现

本文档展示了如何使用Java的javapns库向iOS设备发送推送通知。代码示例中,创建了一个`PushUtils`类,包含了初始化连接、构建推送消息负载、处理推送失败的情况等步骤。通过调用`push`方法,可以向指定的设备tokens发送包含消息内容、角标和可选铃声的推送通知。
摘要由CSDN通过智能技术生成

class="java">package com.malone.common;

import com.google.common.base.Joiner;

import com.google.common.base.Splitter;

import com.google.common.collect.Lists;

import com.malone.exception.OperateFailureException;

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;

import org.apache.commons.lang.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

/**

* Created with IntelliJ IDEA.

* User: malone

* Date: 14-7-19

* Time: 上午10:25

* To change this template use File | Settings | File Templates.

*/

public class PushUtils {

private static Logger logger = LoggerFactory.getLogger(PushUtils.class);

public static void main (String[] args) {

try {

push("2cd260c1876ca094a9452af0dd3595aaead748be0925314050b94ca5061d4f73", "这是测试推送消息");

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 一次给多个手机推送消息

* @param tokens

*/

public static void push (String tokens, String msg) throws Exception {

//发送失败token记录

List failureToken = Lists.newArrayList();

//截取

List tokenList = Splitter.on(",").omitEmptyStrings().splitToList(tokens);

if (tokenList == null || tokenList.size() == 0) {

throw new OperateFailureException("请提供要发送的token!");

}

if (StringUtils.isBlank(msg)) {

throw new OperateFailureException("请提供要发送的消息!");

}

//包装发送消息相关参数

PushNotificationPayload payLoad = new PushNotificationPayload();

payLoad.addAlert(msg); // 消息内容

payLoad.addBadge(PushConsts.badge); // iphone应用图标上小红圈上的数值

if (!StringUtils.isBlank(PushConsts.pushMsgSound)) {

payLoad.addSound(PushConsts.pushMsgSound);//铃音

}

//构造推送服务

PushNotificationManager pushManager = new PushNotificationManager();

//true:表示的是产品发布推送服务 false:表示的是产品测试推送服务

pushManager.initializeConnection(new AppleNotificationServerBasicImpl(PushConsts.certificateLocalPath,

PushConsts.certificatePwd, PushConsts.msgFlag));

List notifications = new ArrayList();

//当单个发送失败,不应该影响其他的发送

for (String token : tokenList) {

try {

PushedNotification notification = pushManager.sendNotification(new BasicDevice(token), payLoad, true);

notifications.add(notification);

} catch (Exception e) {

failureToken.add(token);

logger.error("===============================一次失败开始==================================");

logger.error("token:" + token + ",msg:" + msg + "推送消息失败!");

logger.error(e.getMessage(), e);

e.printStackTrace();

logger.error("===============================一次失败结束==================================");

}

}

//发送消息

// notifications = pushManager.sendNotifications(payLoad, device);

List failedNotifications = PushedNotification.findFailedNotifications(notifications);

List successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);

int failed = failedNotifications.size();

int successful = successfulNotifications.size();

pushManager.stopConnection();

//是否失败

if (failureToken.size() > 0) {

throw new OperateFailureException("如下token推送消息失败:" + Joiner.on(",").join(failureToken.toArray()));

}

if (failed >= 1) {

throw new OperateFailureException("有" + failed + "个消息推送失败!");

}

}

/**

* 推送用到的常量

*/

public static interface PushConsts {

/**

* 推送消息的默认响铃

*/

public String pushMsgSound = "default";

/**

* 图标小红圈的数值

*/

public int badge = 100;

/**

* 推送消息证书的本地路径

*/

public String certificateLocalPath = "F:/ios push/sf push message.p12";

/**

* 推送证书的密码

*/

public String certificatePwd = "sf@push";

/**

* 正式环境推送/开发环境推送

*/

public boolean msgFlag = false;

}

}

?

?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值