uniapp开发小程序消息订阅(和微信api一样 uni.可换成wx.)

需用按钮使用户触发(注释的长期订阅真机调试和测试开发都是好的,但是正式不行,查阅资料是微信做了限制。即使用户点了设置长期接收,微信也会拦截消息通知。如下方法是一次订阅是搭配公众号发消息使用)

<view  @click="getOrdermsg(item.templateId,item.type)" v-if="item.subscribeState==0">
    <button class="cu-btn btn" :loading="loading">订阅一次</button>
</view>
							
<view v-else >
	<button class="cu-btn btn1" :loading="loading">已订阅一次</button>
</view>

getOrdermsg(tmplIds,type){
				let that = this
				uni.getSetting({
				   withSubscriptions: true,//是否获取用户订阅消息的订阅状态,默认false不返回
				   success(res){
					   console.log('sus',res)	
		   //          if(res.subscriptionsSetting[tmplIds]=="accept"){
					// 	uni.showToast({
					// 		icon:'none',
					// 		title:'您已授权长期订阅'
					// 	})
					// 	that.subscribe(type)
					// }
				 //  else 
				  
				  if(res.subscriptionsSetting[tmplIds]=="reject"){
				   			uni.showToast({
				   			icon:'none',
				   			title:'您已拒绝订阅,请点击右上方三个点,找到设置并点击页面的订阅消息选择开启才能继续订阅'
				   			})
				   			return false
				   }else{
					   uni.requestSubscribeMessage({
					     tmplIds: [tmplIds],// 
					     success (res) {
					       if(res[tmplIds] == "accept"){// 字段就是tmplIds模板id
						   uni.showToast({
						   	   icon:'none',
						   	   height:'300',
						   	   title:'你授权一次订阅',
						   	   duration:5000
						      })
						   that.subscribe(type)
					  //       uni.getSetting({
					  //          withSubscriptions: true,//是否获取用户订阅消息的订阅状态,默认false不返回
					  //          success(res){
					  //       	   console.log('sus',res)	
					  //           if(res.subscriptionsSetting[tmplIds]=="accept"){
					  //       		uni.showToast({
					  //       			icon:'none',
					  //       			title:'您已授权长期订阅',
							// 			height:'300',
							// 			duration:5000
					  //       		})
					  //       		that.subscribe(type)
					  //       	}else if(res.subscriptionsSetting[tmplIds]=="reject"){
							// 		 uni.showToast({
							// 		     icon:'none',
							// 		     title:'您已拒绝订阅,请点击右上方三个点,找到设置并点击页面的订阅消息选择开启才能继续订阅',
							// 		 	duration:5000
							// 		 })

				   //            }else{
							// 	  uni.showToast({
							// 	  	icon:'none',
							// 		height:'300',
							// 	  	title:'你授权一次订阅',
							// 		duration:5000
							// 	  })
							//   }
							// }
					  //      })
					     }
					    }
						})
				   }
				   }
				})
			
			},
			subscribe(type){
				 
				this.majax({
					url: this.$local + '',
					params: {
						type:type
					},
					method: 'POST'
				}).then(res => {
					
					if (res.code == 0) {
						uni.showToast({
							icon:'none',
							title:'订阅成功'
						})
						this.getMsg()
					} else {
						this.goout(res)
					}
				})
			}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值