消息推送unipush的简单使用

配置

  1. manifest.json中选中push
    消息推送unipush的简单使用_第1张图片
  2. 配置push
    点击uniPush下的配置
    消息推送unipush的简单使用_第2张图片
    输入个人信息并点击授权并开通
    消息推送unipush的简单使用_第3张图片
    填写信息后点击开通,选择配置管理应用配置,记录应用配置信息
    消息推送unipush的简单使用_第4张图片

前端

在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文件


<dependency>
    <groupId>com.getui.pushgroupId>
    <artifactId>restful-sdkartifactId>
    <version>1.0.0.1version>
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、UserApi
        PushApi 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()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}

调用
使用该方法进行触发,例在收到信息时自动调用等

 UniPushConfig.uniPush(SendId,message,clientId);

3. 打包基座

点击发行,选择原生app-云打包
消息推送unipush的简单使用_第5张图片
选择apk,并填写证书别名、私钥、路径、选择调试基座、选择传统打包
证书申请方法:https://blog.csdn.net/gd898989/article/details/115564462?spm=1001.2014.3001.5501
消息推送unipush的简单使用_第6张图片
等待云打包结束
消息推送unipush的简单使用_第7张图片
点击运行,运行到手机、运行基座选择、自定义基座
消息推送unipush的简单使用_第8张图片
选择后再从新运行到手机设备

进入app应用,控制台打印

13:25:52.841 [Object] {"id":"unipush","token":"f1c3ded44ee0a106ecacf1031152d4da","clientid":"f1c3ded44ee0a106ecacf1031152d4da","...}  at App.vue:5

在这里插入图片描述
将clientId放入uniPush的配置管理故障排查中,点击检测,手机通知栏将会受到信息

消息推送unipush的简单使用_第9张图片

如果后台提示发送成功,手机没有显示信息,打开手机设置 —>应用—>权限管理—>选择基座apk应用—>通知管理,点击允许通知,这样就可以受到信息啦

你可能感兴趣的:(vue前端实用方法,app,java)