Millet Push Java Code
- 2021-06-28 12:29:20
- OfStack
maven
<dependency>
<groupId>com.xiaomi</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.xiaomi</groupId>
<artifactId>MiPush_SDK_Server</artifactId>
<version>2.2.18</version>
</dependency>
java util
package com.tjg99.commons.util;
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Sender;
import com.xiaomi.xmpush.server.Sender.BROADCAST_TOPIC_OP;
import net.sf.json.JSONObject;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Millet Push Tool Class
**/
public class MiPushUtil {
private static String APP_SECRET_KEY_ANDROID = "asdasdasdasdasdasd";
private static String APP_SECRET_KEY_IOS = "asdasdasdasdasd==";
private static String MY_PACKAGE_NAME = "com.tjg99";
/**
* Send push to all devices
* @param messagePayload news
* @param title Message Title
* @param description Message Description
* @param ads_type Message Type
* @param ads_links Message Link
* @throws IOException
* @throws ParseException
**/
public static void sendAllBroadcast(String messagePayload, String title, String description, String ads_type,
String ads_links) throws IOException, ParseException {
Constants.useOfficial();
Map map = new HashMap();
map.put("ads_type", ads_type);
map.put("ads_links", ads_links);
Message message = new Message.Builder().title(title).description(description).payload(messagePayload)
.extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0) // Use default prompt tone prompt
.build();
// Android Push
Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID);
// according to topicList Do union operation , Send message to specified 1 On Group Devices
sender_android.broadcastAll(message, 3);
//ios Push
Sender sender_ios = new Sender(APP_SECRET_KEY_IOS);
// according to topicList Do union operation , Send message to specified 1 On Group Devices
sender_ios.broadcastAll(message, 3);
}
/**
* Specify Label Push ( Push a specified type of user )
* @param messagePayload news
* @param title Message Title
* @param description Message Description
* @param ads_type Message Type
* @param ads_links Message Link
* @param topicList Specify Push Type
* @throws IOException
* @throws ParseException
**/
public static void sendBroadcast(String messagePayload, String title, String description, String ads_type,
String ads_links, List<String> topicList) throws IOException, ParseException {
Constants.useOfficial();
Map map = new HashMap();
map.put("ads_type", ads_type);
map.put("ads_links", ads_links);
Message message = new Message.Builder().title(title).description(description).payload(messagePayload)
.extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0) // Use default prompt tone prompt
.build();
// Android Push
Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID);
// according to topicList Do union operation , Send message to specified 1 On Group Devices
sender_android.multiTopicBroadcast(message, topicList, BROADCAST_TOPIC_OP.UNION, 3);
// ios Push
//Constants.useSandbox();
Sender sender_ios = new Sender(APP_SECRET_KEY_IOS);
// according to topicList Do union operation , Send message to specified 1 On Group Devices
sender_ios.multiTopicBroadcast(message, topicList, BROADCAST_TOPIC_OP.UNION, 3);
}
/**
* Appoint alias Push ( Single or Multiple )
* @param messagePayload news
* @param title Message Title
* @param description Message Description
* @param ads_type Message Type
* @param ads_links Message Link
* @param aliasList Appoint alias
* @throws IOException
* @throws ParseException
**/
public static void sendMessageToAliases(String messagePayload, String title, String description, String ads_type,
String ads_links, List<String> aliasList) throws IOException, ParseException {
Constants.useOfficial();
Map map = new HashMap();
map.put("ads_type", ads_type);
map.put("ads_links", ads_links);
Message message = new Message.Builder().title(title).description(description).payload(messagePayload)
.extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0) // Use default prompt tone prompt
.build();
// Android Push
Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID);
sender_android.sendToAlias(message, aliasList, 3);
//ios Push
//Constants.useSandbox();
Sender sender_ios = new Sender(APP_SECRET_KEY_IOS);
sender_ios.sendToAlias(message, aliasList, 3);
}
}
summary