uniapp 监听推送

<template>
	<view class="page">
		<view class="uni-list">
			<block v-if="listData.length>0">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in listData" :key="key" @click="goDetail(value)"
				 @longpress="show_m(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src="value.face"></image>
						<view v-if="value.stotal>0" class="tip">{{value.stotal}}</view>
						<view class="uni-media-list-body">
							<view class="uni-media-list-text-top"  >{{value.cont}}</view>
							<view class="uni-media-list-text-bottom">
								<block v-if="value.type=='agent'">
									<block v-for="(s,k) in agentarr" :key="k">
										<text v-if="value.id==s.id">{{s.name}}</text>
									</block>
								</block>
								<block v-else>
									<text>{{value.name}}</text>
								</block>
								<text>{{value.optdts}}</text>
							</view>
						</view>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="page-share-title">
					<text>暂无数据</text>
				</view>
			</block>

		</view>
	</view>
</template>
<script>
	import {
		Base64
	} from '../../utils/base64.min.js';
	import service from '../../utils/service.js';

	export default {
		data: {
			listData:'',
			agentarr: '',
			Loop: '',
			ck: true,
			provider: '',
			cont:''
		},

		onLoad() {
			if (!uni.getStorageSync('$APPLOGIN')) {
				uni.reLaunch({
					url: '../login/login'
				});
				return;
			}
			
			this.get_reimarr();
			this.listenTranMsg();
		},
		onShow() {
			//this.get_reimarr();
		},
		methods: {
			listenTranMsg: function() {
				var that = this;
				var provider=uni.getStorageSync('$provider');
				uni.subscribePush({
					provider: provider[0],
					success: (e) => {
// 						uni.showToast({
// 							title: "已开启push接收"
// 						})
					}
				});
				uni.onPush({
					provider: provider[0],
					success: (e) => {
// 						uni.showToast({
// 							title: "开始监听透传数据"
// 						})
					},
					receive: (e) => {
						//that.get_reimarr()
					},
					
				})
				
			},

			get_reimarr: function() {
				uni.showLoading({
					title: '加载中...'
				});
				const data = uni.getStorageSync('$APPLOGIN');
				var that = this;
				service.getURL('?m=index&a=indexapp',data,function(res){
					if (res.data.data.reimarr.length > 0) {
						var da = res.data.data.reimarr;
						that.listData = da;
						that.agentarr = res.data.data.agentarr;
					}
				})
				uni.hideLoading();
			},
			show_m: function(value) {

				var that = this;
				uni.showActionSheet({
					itemList: ['删除记录', '取消'],
					success: function(res) {
						if (res.tapIndex == 0) {
							uni.showModal({
								title: '提示',
								content: '确定要删除会话记录吗?',
								success: function(res) {
									if (res.confirm) {
										const server ='?m=reim&a=delhistory&gid=' + value.receid + '&type=' + value.type;
										const data = uni.getStorageSync('$APPLOGIN');
										
										service.getURL(server,data,function(res){
											
											that.get_reimarr();
											uni.showToast({
												title: '删除成功!',
												duration: 2000
											});
											
										})
									}
								}
							});
						}

					}
				});
			},
			goDetail: function(value) {
				var agentarr = this.agentarr;


				if (value.type == 'user') {
					uni.navigateTo({
						url: 'main-msg?value=' + JSON.stringify(value)
					})
				}
				if (value.type == 'group') {
					uni.showToast({
						title: '没有',
						duration: 2000
					});
				}
				if (value.type == 'agent') {
					for (var i = 0; i < agentarr.length; i++) {
						if (value.receid === agentarr[i]['id']) {
							value.num = agentarr[i]['num'];
							break;
						}
					}
					uni.navigateTo({
						url: 'main-agent?value=' + JSON.stringify(value)
					})
				}
			},
			clearLoop: function() {
				clearInterval(this.Loop);
			}

		}
	}
</script>

<style>
	.tip {
		min-width: 30upx;
		height: 30upx;
		background: red;
		box-sizing: border-box;
		color: white;
		font-size: 16upx;
		text-align: center;
		line-height: 30px;
		padding: 0 0;
		border-radius: 30px;

		position: absolute;
		left: 85upx
	}

	.uni-media-list-logo {
		width: 80upx;
		height: 80upx;
		border-radius: 40upx;
	}

	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		height: 50upx;
		font-size: 28upx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.page-share-title {
		text-align: center;
		font-size: 30upx;
		color: #BEBEBE;
		padding: 20upx 0;
	}

	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
</style>

 

<think>好的,用户想了解如何在UniApp中实现后台推送功能,包括实现方式、文档和示例。我之前整理过相关的引用内容,现在需要结合这些信息来构建一个清晰的回答。 首先,用户提到了后台推送,需要明确UniApp中后台推送通常指的是应用在关闭或后台运行时仍能接收推送消息。根据引用3,uniPush使用个推的服务,通过系统通知栏或厂商通道来展示消息,这应该包括后台推送的情况。引用4提到需要获取供应商信息、订阅推送,并获取token和CID等,这些步骤是实现推送的基础。 接下来,我需要确定实现步骤。根据引用3,步骤包括注册应用实例到个推服务器,获取clientID,然后在DCloud后台管理推送消息。引用4中的代码示例展示了如何获取供应商和订阅推送,但可能需要更详细的说明。需要分步骤讲解配置推送服务、初始化、发送和接收消息。 文档方面,需要引用UniApp官方文档和uniPush的指南,可能还需要提到个推的相关资料。示例代码部分,用户可能需要看到如何初始化推送服务,处理接收消息的事件监听,以及发送推送的示例。 还需要注意不同平台的配置差异,比如Android和iOS可能需要不同的设置,比如厂商通道和证书配置。另外,用户可能关心如何处理点击通知的回调,这也是引用3中提到的内容。 最后,生成相关问题时要围绕实现方式、优化、处理离线情况以及数据统计等方面,帮助用户进一步探索。</think>以下是关于UniApp后台推送功能的实现详解,结合官方文档与实践经验整理: ### 一、实现方式 UniApp后台推送主要依赖**uniPush 2.0**(基于个推服务),其核心流程包含: 1. **设备注册** 应用启动时自动向个推服务器注册,生成唯一设备标识`clientID` $$ clientID = HASH(设备信息 + 时间戳) $$ 2. **消息路由** 通过DCloud开发者中心或自建服务端调用个推API,按`clientID`定向推送[^3] 3. **跨平台分发** Android使用系统级通道(如FCM/厂商通道),iOS通过APNs系统推送 ### 二、配置步骤 #### 1. 基础配置 ```javascript // 初始化推送服务 uni.getProvider({ service: 'push', success: function(res) { if (res.provider.includes('unipush')) { uni.subscribePush({ provider: 'unipush', success: function(res) { console.log('推送订阅成功:', res) } }) } } }) ``` #### 2. 接收消息处理 ```javascript // 监听透传消息(应用在前台) uni.onPushMessage((res) => { console.log('收到推送数据:', res) }) // 处理通知栏点击事件 uni.onNotificationClick((res) => { console.log('通知被点击:', res) uni.navigateTo({ url: '/pages/msg/detail' }) }) ``` ### 三、服务端发送示例(Node.js) ```javascript const axios = require('axios'); // 个推API配置 const GtConfig = { appId: "YOUR_APPID", appKey: "YOUR_APPKEY", masterSecret: "YOUR_SECRET" }; async function sendPush(clientID, content) { const response = await axios.post('https://restapi.getui.com/v2/'+GtConfig.appId+'/push/single/cid', { request_id: Date.now().toString(), audience: { cid: [clientID] }, settings: { ttl: 3600000 }, push_message: { notification: { title: "新消息", body: content, click_type: "startapp" // 点击打开应用 } } }, { headers: { 'Content-Type': 'application/json', 'token': await getToken() // 需实现token获取 } }); return response.data; } ``` ### 四、关键文档参考 1. [UniPush使用指南](https://uniapp.dcloud.io/unipush-v2.html) 2. [个推服务端API文档](https://docs.getui.com/getui/server/rest_v2/) 3. [厂商通道集成说明](https://ask.dcloud.net.cn/article/35622) ### 五、注意事项 1. **iOS证书配置** 需上传APNs证书到DCloud后台,开启`Push Notification`能力 2. **Android厂商通道** 需在`manifest.json`配置小米、华为等平台的AppID/AppKey[^3] 3. **离线消息处理** 消息有效期建议设置`ttl`参数(单位毫秒),超时后个推服务器自动丢弃[^4]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥596607010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值