配置
- manifest.json中选中push
- 配置push
点击uniPush下的配置
输入个人信息并点击授权并开通
填写信息后点击开通,选择配置管理应用配置,记录应用配置信息
前端
在app.vue中
onLaunch: function() {const clientInfo = plus.push.getClientInfo()console.log(clientInfo)plus.push.addEventListener("click", function(msg) {console.log(JSON.stringify(msg));console.log(msg.payload);//这里可以写跳转业务代码}, false);// 监听在线消息事件 plus.push.addEventListener("receive", function(msg) {// plus.ui.alert(2); //这里可以写跳转业务代码console.log("recevice:" + JSON.stringify(msg))}, false);//提示打开权限通知let main = plus.android.runtimeMainActivity();let pkName = main.getPackageName();let NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat"); let packageNames = NotificationManagerCompat.from(main); if (!packageNames.areNotificationsEnabled()) {//手机没有开启通知的权限uni.showModal({ title: '提示', content: '建议大咖消息通知', showCancel: false, success:(res)=> {let uid = main.getApplicationInfo().plusGetAttribute("uid");let Intent = plus.android.importClass('android.content.Intent');let Build = plus.android.importClass("android.os.Build");let Settings = plus.android.importClass("android.provider.Settings");let Uri = plus.android.importClass("android.net.Uri");let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);let uri = Uri.fromParts("package", main.getPackageName(), null);intent.setData(uri);main.startActivity(intent);}})}
},
运行后,打印出
{"id": "unipush","token": "f1c3ded44ee0a***cf1031152d4da","clientid": "f1c3ded44ee0a***cf1031152d4da","appid": "8APxdkKm***mUOYezoSw1","appkey": "pwyoO4ID***SjGrwO3o1H9"
}
调用
java后端
pom文件
<!-- https://mvnrepository.com/artifact/com.getui.push/restful-sdk 推送信息-->
<dependency><groupId>com.getui.push</groupId><artifactId>restful-sdk</artifactId><version>1.0.0.1</version>
</dependency>
创建UniPushConfig
并将前面配置管理下应用配置信息,填写在setAppId,setAppKey,setMasterSecret中
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.Map;
@Component
public class UniPushConfig {public static void uniPush(String title,String content,String cid){GtApiConfiguration apiConfiguration = new GtApiConfiguration();//填写应用配置apiConfiguration.setAppId("8APxdkKmYJ***ezoSw1");apiConfiguration.setAppKey("pwyoO4IDrd***GrwO3o1H9");apiConfiguration.setMasterSecret("yxTjo***9K6BnKr9zF6En4");apiConfiguration.setDomain("https://restapi.getui.com/v2/");// 实例化ApiHelper对象,用于创建接口对象ApiHelper apiHelper = ApiHelper.build(apiConfiguration);// 创建对象,建议复用。目前有PushApi、StatisticApi、UserApiPushApi pushApi = apiHelper.creatApi(PushApi.class);//根据cid进行单推PushDTO<Audience> pushDTO = new PushDTO<Audience>();// 设置推送参数pushDTO.setRequestId(System.currentTimeMillis() + "");//requestid需要每次变化唯一//配置推送条件// 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;// 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;// 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;// 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。Strategy strategy=new Strategy();strategy.setDef(1);Settings settings=new Settings();settings.setStrategy(strategy);pushDTO.setSettings(settings);settings.setTtl(3600000);//消息有效期,走厂商消息需要设置该值
//推送苹果离线通知标题内容Alert alert=new Alert();alert.setTitle("苹果离线通知栏标题");alert.setBody("苹果离线通知栏内容");Aps aps = new Aps();//1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。//苹果建议1小时最多推送3条静默消息aps.setContentAvailable(0);aps.setSound("default");aps.setAlert(alert);IosDTO iosDTO = new IosDTO();iosDTO.setAps(aps);iosDTO.setType("notify");PushChannel pushChannel = new PushChannel();pushChannel.setIos(iosDTO);//安卓离线厂商通道推送消息体
// PushChannel pushChannel = new PushChannel();AndroidDTO androidDTO = new AndroidDTO();Ups ups = new Ups();ThirdNotification notification1 = new ThirdNotification();;ups.setNotification(notification1);notification1.setTitle("安卓离线展示的标题");notification1.setBody("安卓离线展示的内容");notification1.setClickType("intent");notification1.setIntent("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;component=io.dcloud.HBuilder/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题;S.content=测试内容;S.payload=test;end");//各厂商自有功能单项设置
//ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");//ups.addOption("HW", "/message/android/notification/badge/add_num", 1);//ups.addOption("HW", "/message/android/notification/importance", "HIGH");
//ups.addOption("VV","classification",1);androidDTO.setUps(ups);pushChannel.setAndroid(androidDTO);pushDTO.setPushChannel(pushChannel);// PushMessage在线走个推通道才会起作用的消息体PushMessage pushMessage = new PushMessage();pushDTO.setPushMessage(pushMessage);pushMessage.setTransmission(" {title:"+title+",content:"+content+",payload:\"自定义数据\"}");// 设置接收人信息Audience audience = new Audience();pushDTO.setAudience(audience);audience.addCid(cid);// 进行cid单推ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);if (apiResult.isSuccess()) {// successSystem.out.println(apiResult.getData());} else {// failedSystem.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());}}
}
调用
使用该方法进行触发,例在收到信息时自动调用等
UniPushConfig.uniPush(SendId,message,clientId);
3. 打包基座
点击发行,选择原生app-云打包
选择apk,并填写证书别名、私钥、路径、选择调试基座、选择传统打包
证书申请方法:https://blog.csdn.net/gd898989/article/details/115564462?spm=1001.2014.3001.5501
等待云打包结束
点击运行,运行到手机、运行基座选择、自定义基座
选择后再从新运行到手机设备
进入app应用,控制台打印
13:25:52.841 [Object] {"id":"unipush","token":"f1c3ded44ee0a106ecacf1031152d4da","clientid":"f1c3ded44ee0a106ecacf1031152d4da","...} at App.vue:5
将clientId放入uniPush的配置管理故障排查中,点击检测,手机通知栏将会受到信息
如果后台提示发送成功,手机没有显示信息,打开手机设置 —>应用—>权限管理—>选择基座apk应用—>通知管理,点击允许通知,这样就可以受到信息啦