极光推送php服务器代码,《小印记》源码分享--极光推送服务器篇

拼搏现实的明天。 2022-10-21 01:26 270阅读 0赞

笔者这几天刚完成《小印记》的推送功能,今天特分享一下在做的过程中实际解决的问题。如果读者学到了有用的东西,希望能前往App Store下载《小印记》支持一下笔者,谢谢!

前言

笔者建议先去极光推送官网下载-> Demo,因为官网demo里面有需要的 jar 包,笔者这里把需要的jar整理了一下,读者可以直接去下载:https://github.com/Jacedy/jpush-jar

关键源码分享

package com.jk.dao.impl.user;

import java.text.SimpleDateFormat;

import java.util.Date;

import cn.jiguang.common.resp.APIConnectionException;

import cn.jiguang.common.resp.APIRequestException;

import cn.jpush.api.JPushClient;

import cn.jpush.api.push.PushResult;

import cn.jpush.api.push.model.Message;

import cn.jpush.api.push.model.Options;

import cn.jpush.api.push.model.Platform;

import cn.jpush.api.push.model.PushPayload;

import cn.jpush.api.push.model.audience.Audience;

import cn.jpush.api.push.model.notification.AndroidNotification;

import cn.jpush.api.push.model.notification.IosNotification;

import cn.jpush.api.push.model.notification.Notification;

import com.jk.common.base.Result;

import com.google.gson.JsonObject;

import com.jk.dao.user.RemoteNoticeDao;

public class RemoteNoticeDaoImpl implements RemoteNoticeDao {

private static final String appKey =”9de85312fa00e09baf*****“;

private static final String masterSecret = “d42ac8a024b7add0fe*****“;

static JPushClient jPushClient = new JPushClient(masterSecret, appKey);

/**

* 推送给指定的Alias用户

* @param alias 别名

* @param alertMapalert配置(title、subtitle、body)

* @param extraJsonObject额外增加字段

* @return 0推送失败,1推送成功

*/

public Result sendToAlias(String alias,JsonObject alertJsonObject, JsonObject extraJsonObject) {

Result result = new Result();

try {

PushPayload pushPayload= this.buildPushObject_Alias_alertWithTitle(alias,alertJsonObject,extraJsonObject);

System.out.println(pushPayload);

PushResult pushResult=jPushClient.sendPush(pushPayload);

System.out.println(pushResult);

result.setError_no(String.valueOf(pushResult.statusCode));

result.setError_msg(String.valueOf(pushResult.msg_id));

} catch (APIConnectionException e) {

e.printStackTrace();

} catch (APIRequestException e) {

e.printStackTrace();

}

return result;

}

/**

* 推送给指定的Tags用户

* @param Tags tags

* @param alertMapalert配置(title、subtitle、body)

* @param extraJsonObject额外增加字段

* @return 0推送失败,1推送成功

*/

public Result sendToTags(String tags,JsonObject alertJsonObject, JsonObject extraJsonObject) {

Result result = new Result();

try {

PushPayload pushPayload= this.buildPushObject_Tags_alertWithTitle(tags,alertJsonObject,extraJsonObject);

System.out.println(pushPayload);

PushResult pushResult=jPushClient.sendPush(pushPayload);

System.out.println(pushResult);

result.setError_no(String.valueOf(pushResult.statusCode));

result.setError_msg(String.valueOf(pushResult.msg_id));

} catch (APIConnectionException e) {

e.printStackTrace();

} catch (APIRequestException e) {

e.printStackTrace();

}

return result;

}

/**

* 发送给所有安卓用户

* @param alertMapalert配置(title、subtitle、body)

* @param extraJsonObject额外增加字段

* @return 0推送失败,1推送成功

*/

public Result sendToAndroid(JsonObject alertJsonObject, JsonObject extraJsonObject) {

Result result = new Result();

try {

PushPayload pushPayload= this.buildPushObject_android_all_alertWithTitle(alertJsonObject, extraJsonObject);

System.out.println(pushPayload);

PushResult pushResult=jPushClient.sendPush(pushPayload);

System.out.println(pushResult);

result.setError_no(String.valueOf(pushResult.statusCode));

result.setError_msg(String.valueOf(pushResult.msg_id));

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 发送给所有IOS用户

* @param alertMapalert配置(title、subtitle、body)

* @param extraJsonObject额外增加字段

* @return 0推送失败,1推送成功

*/

public Result sendToIOS(JsonObject alertJsonObject, JsonObject extraJsonObject) {

Result result = new Result();

try {

PushPayload pushPayload= this.buildPushObject_ios_all_alertWithTitle(alertJsonObject, extraJsonObject);

System.out.println(pushPayload);

PushResult pushResult=jPushClient.sendPush(pushPayload);

System.out.println(pushResult);

result.setError_no(String.valueOf(pushResult.statusCode));

result.setError_msg(String.valueOf(pushResult.msg_id));

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 发送给所有用户

* @param alertMapalert配置(title、subtitle、body)

* @param extraJsonObject额外增加字段

* @return 0推送失败,1推送成功

*/

public Result sendToAll(JsonObject alertJsonObject, JsonObject extraJsonObject) {

Result result = new Result();

try {

PushPayload pushPayload= this.buildPushObject_android_and_ios(alertJsonObject, extraJsonObject);

System.out.println(pushPayload);

PushResult pushResult=jPushClient.sendPush(pushPayload);

System.out.println(pushResult);

result.setError_no(String.valueOf(pushResult.statusCode));

result.setError_msg(String.valueOf(pushResult.msg_id));

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

public PushPayload buildPushObject_android_and_ios(JsonObject alertJsonObject, JsonObject extraJsonObject) {

System.out.println(“—————buildPushObject_android_and_ios_alertWithTitle”);

extraJsonObject.addProperty(“customId”, this.getCustomId());

return PushPayload.newBuilder()

.setPlatform(Platform.android_ios())

.setAudience(Audience.all())

.setNotification(Notification.newBuilder()

.setAlert(alertJsonObject)

.addPlatformNotification(AndroidNotification.newBuilder()

.setAlert(alertJsonObject)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

.build()

)

.addPlatformNotification(IosNotification.newBuilder()

//传一个IosAlert对象,指定apns body、title、subtitle等

.setAlert(alertJsonObject)

//直接传alert

//此项是指定此推送的badge自动加1

.incrBadge(1)

//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,

// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音

.setSound(“default”)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

//允许后台推送

.setContentAvailable(true)

.build()

)

.build()

)

//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,

// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的

// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别

.setMessage(Message.newBuilder()

.setMsgContent(alertJsonObject.toString())

.setTitle(extraJsonObject.toString())

.build())

.setOptions(Options.newBuilder()

//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义

.setApnsProduction(false)

//此字段是给开发者自己给推送编号,方便推送者分辨推送记录

//.setSendno(1)

//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒

.setTimeToLive(864000)

.build()

)

.build();

}

private PushPayload buildPushObject_android_all_alertWithTitle(JsonObject alertJsonObject, JsonObject extraJsonObject) {

System.out.println(“—————buildPushObject_android_all_alertWithTitle”);

extraJsonObject.addProperty(“customId”, this.getCustomId());

return PushPayload.newBuilder()

//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台

.setPlatform(Platform.android())

//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id

.setAudience(Audience.all())

//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发

.setNotification(Notification.newBuilder()

//指定当前推送的android通知

.addPlatformNotification(AndroidNotification.newBuilder()

.setAlert(alertJsonObject)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

.build())

.build()

)

//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,

// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的

// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别

.setMessage(Message.newBuilder()

.setMsgContent(alertJsonObject.toString())

.setTitle(extraJsonObject.toString())

.build())

.setOptions(Options.newBuilder()

//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义

.setApnsProduction(false)

//此字段是给开发者自己给推送编号,方便推送者分辨推送记录

//.setSendno(1)

//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒

.setTimeToLive(864000)

.build())

.build();

}

private PushPayload buildPushObject_Alias_alertWithTitle(String alias,JsonObject alertJsonObject, JsonObject extraJsonObject) {

System.out.println(“—————buildPushObject_Alias_alert”);

//创建一个IosAlert对象,可指定APNs的alert、title等字段

// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(alertJsonObject.get(“title”).toString(), alertJsonObject.get(“subtitle”).toString(), alertJsonObject.get(“body”).toString()).build();

extraJsonObject.addProperty(“customId”, this.getCustomId());

return PushPayload.newBuilder()

//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台

.setPlatform(Platform.all())

//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id

.setAudience(Audience.alias(alias))

//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发

.setNotification(Notification.newBuilder()

//指定当前推送的android通知

.addPlatformNotification(AndroidNotification.newBuilder()

.setAlert(alertJsonObject)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

.build())

//指定当前推送的iOS通知

.addPlatformNotification(IosNotification.newBuilder()

//传一个IosAlert对象,指定apns title、subtitle、body等

.setAlert(alertJsonObject)

//直接传alert

//此项是指定此推送的badge自动加1

.incrBadge(1)

//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,

// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音

.setSound(“default”)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

//允许后台推送

.setContentAvailable(true)

.build())

.build())

//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,

// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的

// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别

.setMessage(Message.newBuilder()

.setTitle(extraJsonObject.toString())

.setMsgContent(alertJsonObject.toString())

.build())

.setOptions(Options.newBuilder()

//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义

.setApnsProduction(false)

//此字段是给开发者自己给推送编号,方便推送者分辨推送记录

//.setSendno(1)

//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;

.setTimeToLive(864000)

.build())

.build();

}

private PushPayload buildPushObject_Tags_alertWithTitle(String tags, JsonObject alertJsonObject, JsonObject extraJsonObject) {

System.out.println(“—————buildPushObject_Tags_alert”);

//创建一个IosAlert对象,可指定APNs的alert、title等字段

// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(alertJsonObject.get(“title”).toString(), alertJsonObject.get(“subtitle”).toString(), alertJsonObject.get(“body”).toString()).build();

extraJsonObject.addProperty(“customId”, this.getCustomId());

return PushPayload.newBuilder()

//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台

.setPlatform(Platform.all())

//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id

.setAudience(Audience.tag(tags))

//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发

.setNotification(Notification.newBuilder()

//指定当前推送的android通知

.addPlatformNotification(AndroidNotification.newBuilder()

.setAlert(alertJsonObject)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

.build())

//指定当前推送的iOS通知

.addPlatformNotification(IosNotification.newBuilder()

//传一个IosAlert对象,指定apns title、subtitle、body等

.setAlert(alertJsonObject)

//直接传alert

//此项是指定此推送的badge自动加1

.incrBadge(1)

//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,

// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音

.setSound(“default”)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

//允许后台推送

.setContentAvailable(true)

.build())

.build())

//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,

// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的

// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别

.setMessage(Message.newBuilder()

.setTitle(extraJsonObject.toString())

.setMsgContent(alertJsonObject.toString())

.build())

.setOptions(Options.newBuilder()

//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义

.setApnsProduction(false)

//此字段是给开发者自己给推送编号,方便推送者分辨推送记录

//.setSendno(1)

//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;

.setTimeToLive(864000)

.build())

.build();

}

private PushPayload buildPushObject_ios_all_alertWithTitle(JsonObject alertJsonObject, JsonObject extraJsonObject) {

System.out.println(“—————buildPushObject_ios_registrationId_alertWithTitle”);

//创建一个IosAlert对象,可指定APNs的alert、title等字段

// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(alertJsonObject.get(“title”).toString(), alertJsonObject.get(“subtitle”).toString(), alertJsonObject.get(“body”).toString()).build();

extraJsonObject.addProperty(“customId”, this.getCustomId());

return PushPayload.newBuilder()

//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台

.setPlatform(Platform.ios())

//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id

.setAudience(Audience.all())

//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发

.setNotification(Notification.newBuilder()

//指定当前推送的android通知

.addPlatformNotification(IosNotification.newBuilder()

//传一个IosAlert对象,指定apns title、subtitle、body等

.setAlert(alertJsonObject)

//直接传alert

//此项是指定此推送的badge自动加1

.incrBadge(1)

//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,

// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音

.setSound(“default”)

//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)

.addExtra(“extra”, extraJsonObject)

//允许后台推送

.setContentAvailable(true)

.build())

.build()

)

//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。

//jpush的自定义消息,sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的

// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别

.setMessage(Message.newBuilder()

.setTitle(extraJsonObject.toString())

.setMsgContent(alertJsonObject.toString())

.build())

.setOptions(Options.newBuilder()

//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义

.setApnsProduction(false)

//此字段是给开发者自己给推送编号,方便推送者分辨推送记录

//.setSendno(1)

//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒

.setTimeToLive(864000)

.build())

.build();

}

public String getCustomId() {

SimpleDateFormat df = new SimpleDateFormat(“MMddHHmmssS”);

int i = (int)(Math.random()*100);

String customId = String.format(“%s%d”, df.format(new Date()), i);

return customId;

}

}

最后附上《小印记》截图,希望读者多多支持

4a114f3d87b11b346146aa018dd2611c.png

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序--(极光)

    鉴权 开发者在执行初始化的时候,需要传入 auth\_payload。 该数据结构由开发者服务端生成并传回浏览器,用于开发者授权此浏览器运行的 JMessage 初始化。

    相关 极光

    [2019独角兽企业重金招聘Python工程师标准>>> ][2019_Python_] ![hot3.png][] 极光推送 消息推送 转载于:https://my.os