극광 푸 시 자바 배경 통합 인 스 턴 스
19779 단어 자바
2. 해당 하 는 jar 패 키 지 를 자신의 웹 프로젝트 의 lib 에 복사 하고 추가 합 니 다.
3. 그 다음 에 직접 사용 하면 됩 니 다. 오류 가 발생 할 수 있 지만 해결 하기 어렵 지 않 습 니 다. 아래 의 코드 는 거의 모든 상황 을 푸 시 하 는 것 을 포함 합 니 다.
public class JpushClientUtil {
private final static String masterSecret = "";
private final static String appKey = "";
private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
/**
*
* @param registrationId
* @param notification_title
* @param msg_title
* @param msg_content
* @param extrasparam
* @return 0 ,1
*/
public static int sendToRegistrationId(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam,String type) {
int result = 0;
try {
PushPayload pushPayload=null;
if(type.equals("0")){//
pushPayload=JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationList,notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
}else if(type.equals("1")){
pushPayload=JpushClientUtil.buildPushObject(registrationList,notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
}
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
//iOS
public static void testSendIosAlert() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody("test alert", "subtitle", "test ios alert json")
.setActionLocKey("PLAY")
.build();
try {
PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap(), "alias1");
System.out.println("Got result - " + result);
} catch (APIConnectionException e) {
System.out.println("Connection error. Should retry later. "+e);
} catch (APIRequestException e) {
System.out.println("Error response from JPush server. Should review and fix it. "+e);
}
}
/**
*
* @param notification_title
* @param msg_title
* @param msg_content
* @param extrasparam
* @return 0 ,1
*/
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* IOS
* @param notification_title
* @param msg_title
* @param msg_content
* @param extrasparam
* @return 0 ,1
*/
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param notification_title
* @param msg_title
* @param msg_content
* @param extrasparam
* @return 0 ,1
*/
public static int sendToAll( String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.setAlert(notification_title)
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
// , 。 , key (value)
.addExtra("androidNotification extras key",extrasparam)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
// IosAlert , apns title、title、subtitle
.setAlert(notification_title)
// alert
// badge 1
.incrBadge(1)
// default ; sound.caf sound.caf ,
// ; ,iOS9 ,
.setSound("sound.caf")
// , 。 , key (value)
.addExtra("iosNotification extras key",extrasparam)
// background , background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build()
)
.build()
)
//Platform 。 jpush ,
// sdk , 。 http://docs.jpush.io/guideline/faq/
// [ ?]
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
// apns ,false ,true ; android
.setApnsProduction(false)
// ,
.setSendno(1)
// , , ,
.setTimeToLive(86400)
.build()
)
.build();
}
private static PushPayload buildPushObject_all_registrationId_alertWithTitle(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_all_all_alert");
// IosAlert , APNs alert、title
IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody(msg_title,notification_title,"")
.setActionLocKey("PLAY")
.build();
return PushPayload.newBuilder()
// ,all , android
.setPlatform(Platform.all())
// ,all , tag alias registration id
.setAudience(Audience.registrationId(registrationList))
//jpush ,android jpush ,iOS apns ,Winphone mpns
.setNotification(Notification.newBuilder()
// // android
// .addPlatformNotification(AndroidNotification.newBuilder()
// .setAlert(notification_title)
// .setTitle(msg_title)
// // , 。 , key (value)
// .addExtra("extrasparam",extrasparam)
// .build())
// iOS
.addPlatformNotification(IosNotification.newBuilder()
// IosAlert , apns title、title、subtitle
.setAlert(alert)
// alert
// badge 1
.incrBadge(0)
// default ; sound.caf sound.caf ,
// ; ,iOS9 ,
.setSound("default")
// , 。 , key (value)
.addExtra("extrasparam",extrasparam)
// background , background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// , ios
// .setContentAvailable(true)
.build()).build())
//Platform 。 jpush ,
// sdk , 。 http://docs.jpush.io/guideline/faq/
// [ ?]
.setMessage(Message.newBuilder()
.setMsgContent(notification_title)
.setTitle(msg_title)
.addExtra("extrasparam",extrasparam)
.addExtra("userId",msg_content.replaceAll(" ", ""))
.build())
.setOptions(Options.newBuilder()
// apns ,false ,true ; android
.setApnsProduction(false)
// ,
.setSendno(1)
// , , ;
.setTimeToLive(86400)
.build())
.build();
}
private static PushPayload buildPushObject(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_all_all_alert");
// IosAlert , APNs alert、title
IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody("test alert", "subtitle", "test ios alert json")
.setActionLocKey("PLAY")
.build();
return PushPayload.newBuilder()
// ,all , android
.setPlatform(Platform.all())
// ,all , tag alias registration id
.setAudience(Audience.registrationId(registrationList))
//jpush ,android jpush ,iOS apns ,Winphone mpns
.setNotification(Notification.newBuilder()
// android
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(msg_title)
// , 。 , key (value)
.addExtra("extrasparam",extrasparam)
.build())
// iOS
.addPlatformNotification(IosNotification.newBuilder()
// IosAlert , apns title、title、subtitle
.setAlert(alert)
// alert
// badge 1
.incrBadge(1)
// default ; sound.caf sound.caf ,
// ; ,iOS9 ,
.setSound("default")
// , 。 , key (value)
.addExtra("extrasparam",extrasparam)
// background , background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// , ios
// .setContentAvailable(true)
.build())
.build())
//Platform 。 jpush ,
// sdk , 。 http://docs.jpush.io/guideline/faq/
// [ ?]
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
// apns ,false ,true ; android
.setApnsProduction(false)
// ,
.setSendno(1)
// , , ;
.setTimeToLive(86400)
.build())
//
.build();
}
private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
return PushPayload.newBuilder()
// ,all , android
.setPlatform(Platform.android())
// ,all , tag alias registration id
.setAudience(Audience.all())
//jpush ,android jpush ,iOS apns ,Winphone mpns
.setNotification(Notification.newBuilder()
// android
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
// , 。 , key (value)
.addExtra("androidNotification extras key",extrasparam)
.build())
.build()
)
//Platform 。 jpush ,
// sdk , 。 http://docs.jpush.io/guideline/faq/
// [ ?]
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
// apns ,false ,true ; android
.setApnsProduction(false)
// ,
.setSendno(1)
// , , ,
.setTimeToLive(86400)
.build())
.build();
}
private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");
IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody(notification_title, msg_title, msg_content)
.setActionLocKey("PLAY")
.build();
return PushPayload.newBuilder()
// ,all , android
.setPlatform(Platform.ios())
// ,all , tag alias registration id
.setAudience(Audience.all())
//jpush ,android jpush ,iOS apns ,Winphone mpns
.setNotification(Notification.newBuilder()
// android
.addPlatformNotification(IosNotification.newBuilder()
// IosAlert , apns title、title、subtitle
.setAlert(alert)
// alert
// badge 1
.incrBadge(1)
// default ; sound.caf sound.caf ,
// ; ,iOS9 ,
.setSound("sound.caf")
// , 。 , key (value)
.addExtra("iosNotification extras key",extrasparam)
// background , background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build())
.build()
)
//Platform 。 jpush ,
// sdk , 。 http://docs.jpush.io/guideline/faq/
// [ ?]
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
// apns ,false ,true ; android
.setApnsProduction(false)
// ,
.setSendno(1)
// , , ,
.setTimeToLive(86400)
.build())
.build();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.