ios 局域网推送 java_iOS APNS推送远程消息 java后台实现

这篇博客详细介绍了如何在iOS应用中实现局域网推送,并通过Java后台使用APNS进行远程消息推送。包括生成证书、设备注册以及Java后台推送代码的实现过程。
摘要由CSDN通过智能技术生成

准备工作之前得准备三个文件:一个是CSR请求文件(CertificateSigningRequest)、一个是aps_development.cer的SSL证书文件、还有一个是推送的PushDeveloper.p12秘钥文件。如下图所示:

cff37f8c442a9722654eee527f33de36.png

1.生成Certificate Signing Request步骤省略。

2.下载开发证书和发布证书

到苹果开发官网描述下载证书,以开发证书为案例。如下图所示:

ca43cbdfbfc8b1e7c7688de1ec908bf4.png

3.从钥匙串访问中导出密钥

打开钥匙串访问,找到我们的专用密钥(专用秘钥就是我们下载推送证书安装本地的私人密钥),如下图所示:

2805de37ad9501e74211a446b15f4ff9.png

导出.p12文件的时候需要输入密码,我这里选择简单点为123456,这密码需要记住,后面有用到。

802fc8d7c90a332f1c5e185a2a5a4d52.png

上面步骤完成后打开终端

cd到p12目录,就是放那三个文件所在的文件夹目录

1.把aps_development.cer的SSL证书转换为PushChatCert.pem文件,执行命令:

openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem

复制代码

e52f03aeee6280a7a614a18444995713.png

在p12目录下会生成一个PushChatCert.pem文件

2.把PushDeveloper.p12格式的私钥转化成PushChatKey.pem,需要设置密码,密码为abc123

openssl pkcs12 -nocerts -out PushChatKey.pem -in PushDeveloper.p12

复制代码

aa4037b98995adec2804a873a116801e.png

3.用certificate和PushChatKey.pem创建apns_developer_identity.p12文件(java后台配置用到的),需要输入密语:abc123

openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "apns_developer_identity" -out apns_developer_identity.p12

复制代码

b00786268174dbc7e6b8c3db5b6689cd.png

ios工程测试

在AppDelegate里didFinishLaunchingWithOptions函数里写

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings

settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)

categories:nil]];

[[UIApplication sharedApplication] registerForRemoteNotifications];

}else{

//这里还是原来的代码

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:

(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];

}

return YES;

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

NSLog(@"regisger success:%@", [[[[deviceToken description] stringByReplacingOccurrencesOfString:@""withString:@""]stringByReplacingOccurrencesOfString:@" "withString:@""]);

}

//前台收到消息

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

// 处理推送消息

NSLog(@"userinfo:%@",userInfo);

NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

NSLog(@"Registfail%@",error);

NSLog(@"DeviceToken获取失败,原因:%@",error);

}

复制代码

java后台代码

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

int badge = 1; // 图标小红圈的数值

String sound = "default"; // 铃音

String msgCertificatePassword = "123456";//导出证书时设置的密码

//90fb73e94659a1822caa51ca079734de6a7e60e44f260a1bfd1326bb4648d734

String deviceToken = "a1f52f971ca720d6ffc98ce7212c74edf347234ab2cc34fefcb541314e2e13e1"; //手机设备token号

String message = "test push message to ios device11111111111";

List tokens = new ArrayList();

tokens.add(deviceToken);

//java必须要用导出p12文件 php的话是pem文件

String certificatePath = "/Users/xxxx/Desktop/p12/apns_developer_identity.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();

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

pushManager.initializeConnection(new AppleNotificationServerBasicImpl(

certificatePath, msgCertificatePassword, false));

List notifications = new ArrayList();

// 开始推送消息

if (sendCount) {

Device device = new BasicDevice();

device.setToken(deviceToken);

PushedNotification notification = pushManager.sendNotification(

device, payload, true);

notifications.add(notification);

} else {

List devices = new ArrayList();

for (String token : tokens) {

devices.add(new BasicDevice(token));

}

notifications = pushManager.sendNotifications(payload, devices);

}

List failedNotification = PushedNotification

.findFailedNotifications(notifications);

List 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==========消息推送完毕");

}

复制代码

您的pom.xml添加以下依赖项:

com.github.fernandospr

javapns-jdk16

2.3.1

log4j

log4j

1.2.17

复制代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值