Java web服务器极光推送使用方法
极光推送是服务器向安卓设备和IOS设备的某个应用发送通知,使用方法如下。
- 在极光推送官方网站中找到对应的maven包
- Java代码使用方法
import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
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 java.util.HashMap;
import java.util.Map;
/**
* Utils - 移动端推送
*/
public final class JPushUtils {
private static String masterSecret = "";
private static String appKey = "";
/**
* 不可实例化
*/
private JPushUtils() {
}
/**
* 向所有设备发送广播消息
* @param title 标题
* @param content 内容
* @return 结果
*/
public static boolean sendBroadcast(String title, String content) {
Map<String, String> map = new HashMap<>(4);
PushPayload pushPayload = buildPushObject_android_ios(title, content, map);
try {
JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushResult pushResult = jPushClient.sendPush(pushPayload);
System.out.println("---------------------------pushResult--------------------------------");
System.out.println(pushResult);
jPushClient.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 向某个用户发送消息
* @param userId 用户ID
* @param title 标题
* @param content 内容
* @return 结果
*/
public static boolean send(Long userId, String title, String content) {
Map<String, String> map = new HashMap<>(4);
String alias = "user_" + id;
PushPayload pushPayload = buildPushObject_android_ios_byAlias(alias, title, content, map);
try {
JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushResult pushResult = jPushClient.sendPush(pushPayload);
System.out.println("---------------------------pushResult--------------------------------");
System.out.println(pushResult);
jPushClient.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 根据别名向设备推送信息
* @param alias 设备名
* @param title 标题
* @param content 内容
* @param map 附加内容
* @return
*/
private static PushPayload buildPushObject_android_ios_byAlias(String alias, String title, String content, Map<String, String> map) {
return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(alias)).setNotification(Notification.newBuilder().setAlert(content).addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).build()).addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtras(map).build()).build()).setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
}
/**
* 向所有设备推送信息
* @param title 标题
* @param content 内容
* @param map 附加内容
* @return 结果
*/
private static PushPayload buildPushObject_android_ios(String title, String content, Map<String, String> map) {
return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all()).setNotification(Notification.newBuilder().setAlert(content).addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).build()).addPlatformNotification(AndroidNotification.newBuilder().addExtras(map).build()).build()).setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
}
}