<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.3</version>
</dependency>


1 import cn.jiguang.common.ClientConfig; 2 import cn.jpush.api.JPushClient; 3 import cn.jpush.api.push.PushResult; 4 import cn.jpush.api.push.model.Options; 5 import cn.jpush.api.push.model.Platform; 6 import cn.jpush.api.push.model.PushPayload; 7 import cn.jpush.api.push.model.audience.Audience; 8 import cn.jpush.api.push.model.notification.AndroidNotification; 9 import cn.jpush.api.push.model.notification.IosNotification; 10 import cn.jpush.api.push.model.notification.Notification; 11 import com.hikvision.cms.common.config.PropertiesReader; 12 import com.hikvision.cms.common.util.log.LogUtils; 13 import org.springframework.stereotype.Service; 14 15 import javax.annotation.PostConstruct; 16 import java.util.List; 17 18 /** 19 * Created by zhanglongbing on 2017/11/6. 20 */ 21 @Service("JpushService") 22 public class JpushService { 23 private static String MASTER_SECRET = PropertiesReader.getWebProperty("MASTER_SECRET"); 24 private static String APP_KEY = PropertiesReader.getWebProperty("APP_KEY"); 25 private JPushClient jpushClient; 26 27 @PostConstruct 28 public void init(){ 29 ClientConfig clientConfig = ClientConfig.getInstance(); 30 clientConfig.setApnsProduction(true); 31 // clientConfig.setApnsProduction(true); 32 jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig); 33 } 34 35 36 public void sentMsg(String msg,List<String> tokens,String ext,String title){ 37 LogUtils.logInfo("消息推送开始:"); 38 PushPayload payload = PushPayload.newBuilder() 39 .setPlatform(Platform.all()) 40 .setNotification(Notification.newBuilder() 41 .addPlatformNotification(AndroidNotification.newBuilder() 42 .addExtra("ext", ext) 43 .setTitle(title) 44 .setAlert(msg) 45 .build()) 46 .addPlatformNotification(IosNotification.newBuilder() 47 .addExtra("ext", ext) 48 .setAlert(msg).setBadge(0).setSound("happy") 49 .build()) 50 .build()) 51 .setOptions(Options.newBuilder() 52 .setApnsProduction(true)//true-推送生产环境 false-推送开发环境(测试使用参数) 53 .setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数) 54 .build()) 55 .setAudience(Audience.registrationId(tokens)) 56 .build(); 57 try { 58 PushResult result = jpushClient.sendPush(payload); 59 LogUtils.logInfo("消息推送结束:" + result); 60 } catch (Exception e) { 61 LogUtils.logException(e); 62 63 } 64 } 65 /* 66 public static void main(String [] args) 67 { 68 ClientConfig clientConfig = ClientConfig.getInstance(); 69 String ext="hello"; 70 clientConfig.setApnsProduction(true); 71 // clientConfig.setApnsProduction(true); 72 JPushClient jpushClient = new JPushClient("6c856e764cb4fe04dbf49946", "6322c1fa0a69ad60061709ea", null, clientConfig); 73 PushPayload payload = PushPayload.newBuilder() 74 .setPlatform(Platform.all()) 75 .setNotification(Notification.newBuilder() 76 .addPlatformNotification(AndroidNotification.newBuilder() 77 .addExtra("ext", ext) 78 .setTitle("巡店消息") 79 .setAlert("连锁") 80 .build()) 81 .addPlatformNotification(IosNotification.newBuilder() 82 .addExtra("ext", ext) 83 .setAlert("连锁").setBadge(1).setSound("happy") 84 .build()) 85 .build()) 86 .setOptions(Options.newBuilder() 87 .setApnsProduction(true)//true-推送生产环境 false-推送开发环境(测试使用参数) 88 .setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数) 89 .build()) 90 .setAudience(Audience.all()) 91 .build(); 92 try { 93 PushResult result = jpushClient.sendPush(payload); 94 //LogUtils.logInfo("消息推送结束:" + result); 95 System.out.print(result); 96 Thread.sleep(5000); 97 }catch(Exception e){ 98 //LogUtils.logException(e); 99 100 } 101 } 102 */ 103 }
这个token会在手机端登陆的时候我后台插一条数据,然后有token
本文详细介绍了如何在项目中集成并使用极光推送(JPush)服务,包括Maven依赖配置、推送消息的Java代码实现及关键参数设置。通过示例代码展示了如何针对特定设备发送带有自定义扩展字段的消息。
511

被折叠的 条评论
为什么被折叠?



