Integration to prepare

Official related links:

Aurora push official website aurora push document Aurora push Java SDK source address

Enter the official website of aurora Push and click document

The following is the official document

Select the server SDK and download the Java server SDK. Note: The version must be the same as the one you want to integrate

Aurora push introduction and Java API call, mainly see the following part of the push code case can refer to the Java SDK source readme.md documentation

Integrated aurora push

Create a New Springboot project and add the aurora push dependency to POM.xml

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.10</version>
</dependency>
Copy the code

Application. Yml adds the aurora push configuration

# Aurora push configuration
jpush:
  appKey:  xxx  # Unique identifier for aurora Platform applications
  masterSecret: xxx   # masterSecret Is used together with appKey to achieve authentication
  apnsProduction: false   This configuration is only valid for ios notifications (true for production, false for development)
Copy the code

You can log in to the official website to obtain the appKey and masterSecret application information, for example, demo application on the official website

MyJpushClient encapsulates the Aurora client action component class

/** ** Aurora push client **/
@Component
public class MyJpushClient {

    @Value("${jpush.appKey}")
    private String appKey;

    @Value("${jpush.masterSecret}")
    private String masterSecret;

    @Value("${jpush.apnsProduction}")
    private boolean apnsProduction;

    private static JPushClient jPushClient = null;
    private static final int RESPONSE_OK = 200;
    private static final Logger logger = LoggerFactory.getLogger(MyJpushClient.class);

    /** * Get the aurora push client object *@return jPushClient
     */
    public JPushClient getJPushClient(a) {
        if (jPushClient == null) {
            jPushClient = new JPushClient(masterSecret, appKey);
        }
        return jPushClient;
    }

    /** * push to alias list **@paramAlias An alias or alias group *@paramNotificationTitle notificationTitle *@paramMsgTitle Message content title *@paramMsgContent Message content *@paramExtras extended fields */
    public void sendToAliasList(List<String> alias, String notificationTitle, String msgTitle, String msgContent, String extras) {
        PushPayload pushPayload = buildPushObject_all_aliasList_alertWithTitle(alias, notificationTitle, msgTitle, msgContent, extras);
        this.sendPush(pushPayload);
    }

    /** * push to tag list **@paramTagsList Tag or Tag group *@paramNotificationTitle notificationTitle *@paramMsgTitle Message content title *@paramMsgContent Message content *@paramExtras extended fields */
    public void sendToTagsList(List<String> tagsList, String notificationTitle, String msgTitle, String msgContent, String extras) {
        PushPayload pushPayload = buildPushObject_all_tagList_alertWithTitle(tagsList, notificationTitle, msgTitle, msgContent, extras);
        this.sendPush(pushPayload);
    }

    /** ** to all Android users **@paramNotificationTitle notificationTitle *@paramMsgTitle Message content title *@paramMsgContent Message content *@paramExtras extended fields */
    public void sendToAllAndroid(String notificationTitle, String msgTitle, String msgContent, String extras) {
        PushPayload pushPayload = buildPushObject_android_all_alertWithTitle(notificationTitle, msgTitle, msgContent, extras);
        this.sendPush(pushPayload);
    }

    /** * send to all IOS users **@paramNotificationTitle notificationTitle *@paramMsgTitle Message content title *@paramMsgContent Message content *@paramExtras extended fields */
    public void sendToAllIOS(String notificationTitle, String msgTitle, String msgContent, String extras) {
        PushPayload pushPayload = buildPushObject_ios_all_alertWithTitle(notificationTitle, msgTitle, msgContent, extras);
        this.sendPush(pushPayload);
    }

    /** ** to all users **@paramNotificationTitle notificationTitle *@paramMsgTitle Message content title *@paramMsgContent Message content *@paramExtras extended fields */
    public void sendToAll(String notificationTitle, String msgTitle, String msgContent, String extras) {
        PushPayload pushPayload = buildPushObject_android_and_ios(notificationTitle, msgTitle, msgContent, extras);
        this.sendPush(pushPayload);
    }

    /** * send push *@paramPushPayload pushPayload *@returnPush result */
    private PushResult sendPush(PushPayload pushPayload) {
        logger.info("pushPayload={}", pushPayload);
        PushResult pushResult = null;
        try {
            pushResult = this.getJPushClient().sendPush(pushPayload);
            logger.info("" + pushResult);
            if (pushResult.getResponseCode() == RESPONSE_OK) {
                logger.info("push successful, pushPayload={}", pushPayload);
            }
            // When the request is complete, call the close method in NettyHttpClient, otherwise the process will not exit.
            this.getJPushClient().close();
        } catch (APIConnectionException e) {
            logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);
        } catch (APIRequestException e) {
            logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);
        }

        return pushResult;
    }
    
    /** * push messages to all users on all platforms **@param notificationTitle
     * @param msgTitle
     * @param msgContent
     * @param extras
     * @return* /
    public PushPayload buildPushObject_android_and_ios(String notificationTitle, String msgTitle, String msgContent, String extras) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(notificationTitle)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notificationTitle)
                                .setTitle(notificationTitle)
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("androidNotification extras key", extras)
                                .build()
                        )
                        .addPlatformNotification(IosNotification.newBuilder()
                                // Pass an IosAlert object specifying apns title, title, subtitle, etc
                                .setAlert(notificationTitle)
                                // Pass alert directly
                                // This badge specifies that the badge for this push should automatically add 1
                                .incrBadge(1)
                                // The value of this field default represents the system default sound; Sound. Caf means that this push is notified by the sound. Caf sound that is packaged in the project.
                                // If the system does not have this audio, the system will use the default sound reminder; If this field is an empty string, iOS9 and later will have no sound reminder, the following system is the default sound
                                .setSound("default")
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("iosNotification extras key", extras)
                                / / this shows that the push is a background push, want to understand the background to see: http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                // .setContentAvailable(true)
                                .build()
                        )
                        .build()
                )
                // Platform specifies which platforms are to be pushed as eligible devices in the specified Platform. Custom messages for JPush,
                // The SDK does nothing by default. Advice to see document of http://docs.jpush.io/guideline/faq/
                // what is the difference between notifications and custom messages? Understand the difference between notifications and custom messages
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .setTitle(msgTitle)
                        .addExtra("message extras key", extras)
                        .build())
                .setOptions(Options.newBuilder()
                        // The value of this field is used to specify the apNS environment to be pushed by this push, false means development, true means production; Meaningless for Android and custom messages
                        .setApnsProduction(apnsProduction)
                        // This field is for the developers to give their own push number, convenient push to distinguish the record
                        .setSendno(1)
                        // The value of this field is used to specify the offline saving time of this push. If this field is not uploaded, the default saving time is one day, and the maximum retention time is ten days, in seconds
                        .setTimeToLive(86400)
                        .build())
                .build();
    }


    /** * Push a message to one or more specified alias users ** across all platforms@param aliasList
     * @param notificationTitle
     * @param msgTitle
     * @param msgContent
     * @param extras
     * @return* /
    private PushPayload buildPushObject_all_aliasList_alertWithTitle(List<String> aliasList, String notificationTitle, String msgTitle, String msgContent, String extras) {
        // Create an IosAlert object that specifies the alert, title, and other fields of APNs
        // IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();

        return PushPayload.newBuilder()
                // Specify the platform to push to. All represents all platforms configured for the current application, or specific platforms such as Android
                .setPlatform(Platform.all())
                // Specify the object to be pushed. All represents the owner. You can also specify the tag or alias that has been set successfully or the registration ID obtained by the application client call interface
                .setAudience(Audience.alias(aliasList))
                // Jpush notifications are delivered directly by JPUSH for Android, APNS server for iOS, and MPNS for Winphone
                .setNotification(Notification.newBuilder()
                        // Specify the current pushed Android notification
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notificationTitle)
                                .setTitle(notificationTitle)
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("androidNotification extras key", extras)
                                .build())
                        // Specify the current iOS notification
                        .addPlatformNotification(IosNotification.newBuilder()
                                // Pass an IosAlert object specifying apns title, title, subtitle, etc
                                .setAlert(notificationTitle)
                                // Pass alert directly
                                // This badge specifies that the badge for this push should automatically add 1
                                .incrBadge(1)
                                // The value of this field default represents the system default sound; Sound. Caf means that this push is notified by the sound. Caf sound that is packaged in the project.
                                // If the system does not have this audio, the system will use the default sound reminder; If this field is an empty string, iOS9 and later will have no sound reminder, the following system is the default sound
                                .setSound("default")
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("iosNotification extras key", extras)
                                / / this shows that the push is a background push, want to understand the background to see: http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                // Uncomment this comment and ios will not be able to receive notifications when they are pushed with a locked screen
                                // .setContentAvailable(true)
                                .build())
                        .build())
                // Platform specifies which platforms are to be pushed as eligible devices in the specified Platform. Custom messages for JPush,
                // The SDK does nothing by default. Advice to see document of http://docs.jpush.io/guideline/faq/
                // what is the difference between notifications and custom messages? Understand the difference between notifications and custom messages
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .setTitle(msgTitle)
                        .addExtra("message extras key", extras)
                        .build())
                .setOptions(Options.newBuilder()
                        // The value of this field is used to specify the apNS environment to be pushed by this push, false means development, true means production; Meaningless for Android and custom messages
                        .setApnsProduction(apnsProduction)
                        // This field is for the developers to give their own push number, convenient push to distinguish the record
                        .setSendno(1)
                        // The value of this field is used to specify the offline saving time of this push. If this field is not uploaded, the default saving time is one day, and the maximum retention time is 10 days.
                        .setTimeToLive(86400)
                        .build())
                .build();

    }

    /** * Push a message to one or more specified Tag users ** across all platforms@param tagsList
     * @param notificationTitle
     * @param msgTitle
     * @param msgContent
     * @param extras
     * @return* /
    private PushPayload buildPushObject_all_tagList_alertWithTitle(List<String> tagsList, String notificationTitle, String msgTitle, String msgContent, String extras) {
        // Create an IosAlert object that specifies the alert, title, and other fields of APNs
        //IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();

        return PushPayload.newBuilder()
                // Specify the platform to push to. All represents all platforms configured for the current application, or specific platforms such as Android
                .setPlatform(Platform.all())
                // Specify the object to be pushed. All represents the owner. You can also specify the tag or alias that has been set successfully or the registration ID obtained by the application client call interface
                .setAudience(Audience.tag(tagsList))
                // Jpush notifications are delivered directly by JPUSH for Android, APNS server for iOS, and MPNS for Winphone
                .setNotification(Notification.newBuilder()
                        // Specify the current pushed Android notification
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notificationTitle)
                                .setTitle(notificationTitle)
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("androidNotification extras key", extras)
                                .build())
                        // Specify the current iOS notification
                        .addPlatformNotification(IosNotification.newBuilder()
                                // Pass an IosAlert object specifying apns title, title, subtitle, etc
                                .setAlert(notificationTitle)
                                // Pass alert directly
                                // This badge specifies that the badge for this push should automatically add 1
                                .incrBadge(1)
                                // The value of this field default represents the system default sound; Sound. Caf means that this push is notified by the sound. Caf sound that is packaged in the project.
                                // If the system does not have this audio, the system will use the default sound reminder; If this field is an empty string, iOS9 and later will have no sound reminder, the following system is the default sound
                                .setSound("default")
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("iosNotification extras key", extras)
                                / / this shows that the push is a background push, want to understand the background to see: http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                // Uncomment this comment and ios will not be able to receive notifications when they are pushed with a locked screen
                                // .setContentAvailable(true)
                                .build())
                        .build())
                // Platform specifies which platforms are to be pushed as eligible devices in the specified Platform. Custom messages for JPush,
                // The SDK does nothing by default. Advice to see document of http://docs.jpush.io/guideline/faq/
                // what is the difference between notifications and custom messages? Understand the difference between notifications and custom messages
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .setTitle(msgTitle)
                        .addExtra("message extras key", extras)
                        .build())
                .setOptions(Options.newBuilder()
                        // The value of this field is used to specify the apNS environment to be pushed by this push, false means development, true means production; Meaningless for Android and custom messages
                        .setApnsProduction(apnsProduction)
                        // This field is for the developers to give their own push number, convenient push to distinguish the record
                        .setSendno(1)
                        // The value of this field is used to specify the offline saving time of this push. If this field is not uploaded, the default saving time is one day, and the maximum retention time is 10 days.
                        .setTimeToLive(86400)
                        .build())
                .build();

    }


    /** * Push a message to all Android users **@param notificationTitle
     * @param msgTitle
     * @param msgContent
     * @param extras
     * @return* /
    private PushPayload buildPushObject_android_all_alertWithTitle(String notificationTitle, String msgTitle, String msgContent, String extras) {
        return PushPayload.newBuilder()
                // Specify the platform to push to. All represents all platforms configured for the current application, or specific platforms such as Android
                .setPlatform(Platform.android())
                // Specify the object to be pushed. All represents the owner. You can also specify the tag or alias that has been set successfully or the registration ID obtained by the application client call interface
                .setAudience(Audience.all())
                // Jpush notifications are delivered directly by JPUSH for Android, APNS server for iOS, and MPNS for Winphone
                .setNotification(Notification.newBuilder()
                        // Specify the current pushed Android notification
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notificationTitle)
                                .setTitle(notificationTitle)
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("androidNotification extras key", extras)
                                .build())
                        .build()
                )
                // Platform specifies which platforms are to be pushed as eligible devices in the specified Platform. Custom messages for JPush,
                // The SDK does nothing by default. Advice to see document of http://docs.jpush.io/guideline/faq/
                // what is the difference between notifications and custom messages? Understand the difference between notifications and custom messages
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .setTitle(msgTitle)
                        .addExtra("message extras key", extras)
                        .build())

                .setOptions(Options.newBuilder()
                        // The value of this field is used to specify the apNS environment to be pushed by this push, false means development, true means production; Meaningless for Android and custom messages
                        .setApnsProduction(apnsProduction)
                        // This field is for the developers to give their own push number, convenient push to distinguish the record
                        .setSendno(1)
                        // The value of this field is used to specify the offline saving time of this push. If this field is not uploaded, the default saving time is one day, and the maximum retention time is ten days, in seconds
                        .setTimeToLive(86400)
                        .build())
                .build();
    }


    /** * Push messages to all ios users **@param notificationTitle
     * @param msgTitle
     * @param msgContent
     * @param extras
     * @return* /
    private PushPayload buildPushObject_ios_all_alertWithTitle(String notificationTitle, String msgTitle, String msgContent, String extras) {
        return PushPayload.newBuilder()
                // Specify the platform to push to. All represents all platforms configured for the current application, or specific platforms such as Android
                .setPlatform(Platform.ios())
                // Specify the object to be pushed. All represents the owner. You can also specify the tag or alias that has been set successfully or the registration ID obtained by the application client call interface
                .setAudience(Audience.all())
                // Jpush notifications are delivered directly by JPUSH for Android, APNS server for iOS, and MPNS for Winphone
                .setNotification(Notification.newBuilder()
                        // Specify the current pushed Android notification
                        .addPlatformNotification(IosNotification.newBuilder()
                                // Pass an IosAlert object specifying apns title, title, subtitle, etc
                                .setAlert(notificationTitle)
                                // Pass alert directly
                                // This badge specifies that the badge for this push should automatically add 1
                                .incrBadge(1)
                                // The value of this field default represents the system default sound; Sound. Caf means that this push is notified by the sound. Caf sound that is packaged in the project.
                                // If the system does not have this audio, the system will use the default sound reminder; If this field is an empty string, iOS9 and later will have no sound reminder, the following system is the default sound
                                .setSound("default")
                                // This field is pass-through and will not be displayed in the notification bar. The user can use this field to do some custom requirements, such as a specific key pass to specify the page (value) to jump to.
                                .addExtra("iosNotification extras key", extras)
                                / / this shows that the push is a background push, want to understand the background to see: http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                // .setContentAvailable(true)
                                .build())
                        .build()
                )
                // Platform specifies which platforms are to be pushed as eligible devices in the specified Platform. Custom messages for JPush,
                // The SDK does nothing by default. Advice to see document of http://docs.jpush.io/guideline/faq/
                // what is the difference between notifications and custom messages? Understand the difference between notifications and custom messages
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .setTitle(msgTitle)
                        .addExtra("message extras key", extras)
                        .build())
                .setOptions(Options.newBuilder()
                        // The value of this field is used to specify the apNS environment to be pushed by this push, false means development, true means production; Meaningless for Android and custom messages
                        .setApnsProduction(apnsProduction)
                        // This field is for the developers to give their own push number, convenient push to distinguish the record
                        .setSendno(1)
                        // The value of this field is used to specify the offline saving time of this push. If this field is not uploaded, the default saving time is one day, and the maximum retention time is ten days, in seconds
                        .setTimeToLive(86400)
                        .build())
                .build();
    }

Copy the code

Test push, using alias push as an example

@RunWith(SpringRunner.class)
@SpringBootTest
class JpushDemoApplicationTests {

    @Autowired
    private MyJPushClient myJPushClient;
    private static final Logger logger = LoggerFactory.getLogger(JpushDemoApplicationTests.class);

    @Test
    void myJPushClientTest(a) {
        List<String> aliasList = Arrays.asList("26143");
        String notificationTitle = "notification_title1707";
        String msgTitle = "msg_title";
        String msgContent = "msg_content";
        myJPushClient.sendToAliasList(aliasList, notificationTitle, msgTitle, msgContent, "exts"); }}Copy the code

Message sent successfully


The paper come zhongjue shallow, and must know this to practice.