Java integrated jpush for Client push

Source: Internet
Author: User
Tags getmessage

The code address is as follows:
Http://www.demodashi.com/demo/13700.html

Objective

Java integrated jpush for Client push

First, the preparatory work

Development environment:
jdk1.6
Eclipse Luna Service Release 1 (4.4.1)
Operating Environment:
Eclipse

Second, Jpush push instructions

Jpush Push is a one-stop push service offered by domestic service providers (both iOS and Android), followed by the ability to use instant messaging for apps. Committed to creating a simple, reliable, competitively priced service (simple functions all free, premium version only charges), so that application developers can focus on business development, push related technology implementation all through the Aurora push to solve, only need to call the Aurora push API can

Three, the principle of push

Android Client Push principle

Jpush WP push includes 1 parts, MPNs push (proxy).

The red part is MPNs push, Jpush agent developer app, push to Microsoft MPNs server. Pushed to the WP device by Microsoft MPNs Server.

The blue section is the Jpush in-app push section, but currently does not support in-app messages.

Apple Client Push principle

As can be seen from the figure, Jpush IOS push includes 2 parts, APNs push (proxy), and Jpush in-app messages.

The red part is APNs push, Jpush agent developer app (needs to be based on app certificate provided by developer), push to Apple APNs server. Pushed to IOS devices by APNs Server.

The blue part is the Jpush in-app push section, when the app starts, the embedded Jpush SDK turns on long connections to Jpush server, so Jpush server can push messages to the app.

IV. Structure of the Code

MySQL table structure

V. Implementation of the service-side program

1, push basically divided into Android and Apple.

Android("android"),IOS("ios"),WinPhone("winphone");private final String value;private DeviceType(final String value) {    this.value = value;}public String value() {    return this.value;}

2. Define push interfaces to push individual users, multiple users, individual devices, and the entire app.

public void pushToUser(String type,String userId, PushEntity pushEntity);public void pushToUserList(String type,List<String> userIdList, PushEntity pushEntity);public void pushToDevice(String type,List<String> deviceTokenList, PushEntity pushEntity);public void pushToApp(String type,PushEntity pushEntity);

3, push the environment to distinguish, Android does not differentiate between development and production environment, Apple needs to differentiate.

        List<String[]> JpushInfoList = MobilePushService.getJpushKeyInfo(type,pushEntity.getJpushApiMasterSecret(),                pushEntity.getJpushAppKey());        // 如果配置mobile.notify.ios.production=false,则是开发模式        boolean iosMode = true;        // 设置平台        payloadBuilder.setPlatform(deviceType.equals(DeviceType.IOS) ? Platform.ios() : Platform.android());        Map<String, Object> extrasMap = new HashMap<String, Object>();

4, integrated Jpush API to implement push function.

            try {                JPushClient jPushClient = new JPushClient(jpushInfo[0], jpushInfo[1], iosMode,                        (pushEntity.getJpushTimeToLive() == null ? 86400 : pushEntity.getJpushTimeToLive()));                jPushClient.sendPush(pushPayload);            } catch (Exception e) {                // 个推时如果手机端没有注册用户,不打错误日志                if (e.getMessage().indexOf("\"code\": 1011") == -1) {                    logger.error("JPUSH推送消息时发生异常:[" + e.getMessage() + "]", e);                }            }        

5. Configure Jpush key with secret dynamic change configuration via MySQL.

        // 如果设定了自定义key,则使用自定义,否则进行数据库查询        if (StringUtils.isNotBlank(apiMasterSecret) && StringUtils.isNotBlank(appKey)) {            resultA.add(new String[] { apiMasterSecret, appKey });        } else {            resultList = CptNotifyJpush.dao.findCptNotifyJpush(type);            if (resultList != null && resultList.size() > 0) {                for (CptNotifyJpush result : resultList) {                    resultA.add(new String[] {result.getStr("api_master_secret"),result.getStr("app_key") });                }            }        }
VI. Client Integration steps

Step1: Go to Aurora to push a registered account: https://www.jpush.cn/, and register the app.
Step2: Upload APNs certificate to Aurora, APNs Certificate Generation Step Reference:
http://docs.jpush.io/client/ios_tutorials/#ios_1
Read the document carefully, the application details of the console after uploading successfully will show "verified", stating that the certificate is valid.

Step3: Reconfigure your app project to use the new provision file that supports APNs (if the previous app already supports APNs, you don't need to change it), you won't be able to get device token later.
STEP4: Integrates its SDK, including a. A and a. h file, the latest version is about 2.1, the SDK also contains the demo, note that before the 2.1 version, you need to create a plist file to hold the secret key information, etc., in the latest version of this file is not required.
Sdk:https://www.jpush.cn/common/products#product-download

STEP5: The main code is modified as follows:
Appdelegate.h:

static NSString AppKey = @ "Your_app_key";
Static NSString
channel = @ "Your_channel_identifier";
static BOOL isproduction = NO;

Seven, the Operation method
public static void main(String args[]){        JPushService pushService=new JPushService();        PushEntity pushEntity=new PushEntity();    //修改密钥    pushEntity.setJpushApiMasterSecret("0a35d2fabea1df2dfc36d32l");    pushEntity.setJpushAppKey("a344debcf41e5542b291d52f");    pushEntity.setMsgContent("test jpush");    pushEntity.setMsgTitle("test jpush");        pushService.sendNotification2App("",pushEntity, DeviceType.IOS);    } pushEntity.setJpushAppKey("a344debcf41e5542b291d52f");    pushEntity.setMsgContent("test jpush");    pushEntity.setMsgTitle("test jpush");        pushService.sendNotification2App("",pushEntity, DeviceType.IOS);java集成jpush实现客户端推送

The code address is as follows:
Http://www.demodashi.com/demo/13700.html

Note: This copyright belongs to the author, by the demo master, refused to reprint, reprint need the author authorization

Java integrated jpush for Client push

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.