第一步,需要先去小程序官方挑選一下訂閱模板拿到模板id
?
訂閱按鈕在頭部導(dǎo)航上,所以文章來源:http://www.zghlxwxcb.cn/news/detail-639529.html
<u-navbar :bgColor="bgColor">
<view class="u-nav-slot" slot="left" @click="goSubscribe">
<image :src="getimg('subscribe.png')" style="width: 44rpx;height: 44rpx;"></image>
<view class="le_tt">訂閱</view>
</view>
</u-navbar>
?文章來源地址http://www.zghlxwxcb.cn/news/detail-639529.html
// 訂閱
goSubscribe() {
// 獲取時(shí)間
let time = new Date()
let today = time.getDate()
let moni1 = time.setDate(today + 1);
let yM2 = new Date(moni1).getFullYear() + '-' + (this.checkTime(new Date(moni1).getMonth() + 1)) + '-'
let temp2 = yM2 + this.checkTime(new Date(moni1).getDate()) //明天
let _this = this;
let tempid = ['你的模板id']
uni.getSetting({
withSubscriptions: true,
success(res) {
// 通過Object.keys() 獲取返回的數(shù)據(jù)包含了哪些鍵名,返回的類型是個(gè)數(shù)組
let obj = Object.keys(res.subscriptionsSetting)
// mainSwitch 是消息訂閱的總開關(guān)
if (!res.subscriptionsSetting.mainSwitch) {
uni.showToast({
title: '請去 "設(shè)置-消息訂閱" 開啟消息通知',
icon: 'none',
duration: 2000
});
} else {
if (obj.includes(tempid[0])) { //判斷是否勾選了“總是保持以上選擇……”
addSubscribe({
date: temp2
}).then(res => {
})
uni.showToast({
title: '您已經(jīng)設(shè)置了總是提醒了喲',
icon: 'none',
duration: 2000
});
return
} else {
_this.isSubscribe = true
uni.requestSubscribeMessage({
tmplIds: tempid,
success(res) {
if (res[tempid[0]] == 'accept') {
addSubscribe({
date: temp2
}).then(res => {
uni.showToast({
icon: 'none',
title: res.msg,
duration: 2000
});
})
_this.isSubscribe = false
} else {
_this.isSubscribe = false
}
},
fail(err) {
console.log(err);
}
})
}
}
}
})
},
到了這里,關(guān)于uniapp 微信小程序 訂閱消息的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!