您的位置:首页 > 编程语言 > Java开发

极光推送JPush java服务端代码

2016-11-21 19:08 459 查看
1、根据别名推送Android设备

public void sendNotificationWirhAlias_Android(String title,String notification,String alias){
try {
PushPayload payload = PushPayload
.newBuilder()
.setPlatform(Platform.android_winphone())
.setAudience(Audience.alias(alias.replace(".", "_")))
.setNotification(Notification.newBuilder()
.setAlert(notification)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.build())
.build();
PushResult result = client.sendPush(payload);
System.out.println(result.isResultOK());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}

2、根据别名推送到iOS设备

public void sendNotificationWirhAlias_Ios(String notification,String alias)
{
try {
PushPayload payload = PushPayload
.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(alias.replace(".", "_")))
.setNotification(Notification.newBuilder()
.setAlert(notification)
.addPlatformNotification(IosNotification.newBuilder().setSound("happy.caf").setBadge(1).build())
.build()).setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
PushResult result = client.sendPush(payload);
System.out.println(result.isResultOK());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}

3、根据别名推送到所有平台

public boolean sendNotificationWithAlias(String title,String notification,String alias,Map<String,String> extras){
try {

PushPayload payload = PushPayload
.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(alias.replace(".", "_")))
.setNotification(
Notification
.newBuilder()
.addPlatformNotification(
IosNotification.newBuilder()
.setAlert(notification)
.setSound("happy.caf").setBadge(1)
.addExtras(extras).build())
.addPlatformNotification(
AndroidNotification.newBuilder()
.setAlert(notification)
.setTitle(title).addExtras(extras)
.build())
.addPlatformNotification(
WinphoneNotification.newBuilder()
.setAlert(notification)
.addExtras(extras).build())
.build()).setOptions(Options.newBuilder()
.setApnsProduction(true)
.build()).build();

PushResult result =client.sendPush(payload);
System.out.println(result.isResultOK());
return result.isResultOK();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return false;
}

来自:http://blog.csdn.net/censhenping/article/details/52217613
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: