一、准備工作
1、開通訂閱消息
進入微信公眾平台(地址:https://mp.weixin.qq.com/),進入小程序【管理后台】>【功能】>【訂閱消息】>開通。
2、申請推送模板
進入小程序【訂閱消息】>【公共模板庫】,搜索選擇自己需要訂閱的消息模板,一個小程序可以訂閱多個消息模板。
二、獲取用戶openid
1、創建雲函數,命名為getOpenid(用於獲取用戶的openid)
2、雲函數的代碼
1 // 雲函數入口文件 2 const cloud = require('wx-server-sdk') 3 4 //cloud.init() 5 //環境變量初始化 6 cloud.init({ 7 evn:cloud.DYNAMIC_CURRENT_ENV //標志當前所在環境 8 }) 9 // 雲函數入口函數 10 exports.main = async (event, context) => { 11 const wxContext = cloud.getWXContext() 12 13 return { 14 event, 15 openid: wxContext.OPENID, //獲取用戶openid 16 appid: wxContext.APPID, 17 unionid: wxContext.UNIONID, 18 } 19 }
3、創建小程序頁面
4、調用雲函數,獲取用戶的openid(console.log打印出來)
- MessagePush.wxml
1 <!-- 獲取用戶openid --> 2 <button bindtap="getOpenid">獲取用戶openid</button>
- MessagePush.js
1 Page({ 2 //獲取用戶的openid 3 getOpenid(){ 4 wx.cloud.callFunction({ 5 name:"getOpenid" 6 }).then(res=>{ 7 console.log("獲取openid成功",res); 8 }).catch(err=>{ 9 console.log("獲取openid失敗",err); 10 }) 11 },12 })
三、獲取用戶授權
MessagePush.wxml
1 <!-- 獲取用戶授權 --> 2 <button bindtap="getAuthorization">獲取用戶授權</button>
MessagePush.js
1 //獲取用戶消息推送授權 2 getAuthorization(){ 3 wx.requestSubscribeMessage({ 4 tmplIds: ["nITTI4zOSJy3C7EtabjhYjWNkf9OrZ9p5cJQaOpNPYw"], //所選模板的模板ID,可以有多個 5 }).then(res=>{ 6 console.log("獲取消息推送授權成功",res); 7 }).catch(err=>{ 8 console.log("獲取消息推送授權失敗",err); 9 }) 10 },
四、創建發送消息的雲函數
1、創建發送消息的雲函數sendMessage
雲函數的index.js代碼
注意:雲函數的編寫要根據模板中的內容來寫
五、發送消息給用戶
包括發送消息給單個用戶和發送消息給多個用戶(此為示例講解),整體代碼:
- 雲函數的代碼
- 雲函數getOpenid(用來獲取用戶的openid)
1 // 雲函數入口文件 2 const cloud = require('wx-server-sdk') 3 4 //cloud.init() 5 //環境變量初始化 6 cloud.init({ 7 evn:cloud.DYNAMIC_CURRENT_ENV //標志當前所在環境 8 }) 9 // 雲函數入口函數 10 exports.main = async (event, context) => { 11 const wxContext = cloud.getWXContext() 12 return { 13 event, 14 openid: wxContext.OPENID, //獲取用戶openid 15 appid: wxContext.APPID, 16 unionid: wxContext.UNIONID, 17 } 18 }
2.雲函數sendMessage(用來發送商品秒殺的訂閱消息)
1 // 雲函數入口文件 2 const cloud = require('wx-server-sdk') 3 4 cloud.init({ 5 env: cloud.DYNAMIC_CURRENT_ENV 6 }) 7 8 // 雲函數入口函數 9 exports.main = async(event, context) => { 10 try { 11 const result = await cloud.openapi.subscribeMessage.send({ 12 touser: event.openid, //要推送給那個用戶 13 page: 'pages/index/index', //要跳轉到那個小程序頁面 14 data: { //推送的內容 15 thing1: { 16 value: event.name //商品名稱 17 }, 18 time2: { 19 value: event.time //秒殺時間 20 }, 21 thing3: { 22 value: event.remark //溫馨提示 23 } 24 }, 25 templateId: 'nITTI4zOSJy3C7EtabjhYjWNkf9OrZ9p5cJQaOpNPYw' //模板id(示例為商品秒殺的消息推送模板),模板ID要與授權的模板ID一致 26 }) 27 console.log(result) 28 return result 29 } catch (err) { 30 console.log(err) 31 return err 32 } 33 }
- 小程序頁面的代碼
- MessagePush.wxml
1 <view>秒殺活動的消息推送</view> 2 <!-- 獲取用戶openid --> 3 <button bindtap="getOpenid" type="primary">獲取用戶openid</button> 4 <!-- 獲取用戶授權 --> 5 <button bindtap="getAuthorization" type="primary">獲取用戶授權</button> 6 <!-- 獲取用戶輸入的 --> 7 <input type="text" placeholder="請輸入商品名稱" bindinput="getName" ></input> 8 <input type="text" placeholder="請輸入秒殺時間" bindinput="getTime"></input> 9 <input type="text" placeholder="請輸入溫馨提示" bindinput="getRemark"></input> 10 <!-- 發送消息給單個用戶 --> 11 <button bindtap="sendMsgToOne" type="primary">發送消息給單用戶</button> 12 <!-- 發送消息給多個用戶 --> 13 <button bindtap="sendMsgToAll" type="primary">發送消息給多用戶</button>
2.MessagePush.wxss
1 button{ 2 margin-top: 20rpx; 3 } 4 5 input{ 6 margin-top: 20rpx; 7 border: 1rpx solid #c3c3c3; 8 height: 80rpx; 9 width: 80%; 10 margin: 20rpx auto ; 11 }
3.MessagePush.js
1 //自定義變量,存儲獲取的openid 2 let openid = '' 3 //自定義變量,存儲用戶輸入的商品名稱/秒殺時間/溫馨提示 4 let name = '' 5 let time = '' 6 let remark = '' 7 Page({ 8 9 //獲取用戶的openid 10 getOpenid(){ 11 wx.cloud.callFunction({ 12 name:"getOpenid" 13 }).then(res=>{ 14 console.log("獲取openid成功",res); 15 openid = res.result.openid 16 console.log("獲取的openid為",openid); 17 }).catch(err=>{ 18 console.log("獲取openid失敗",err); 19 }) 20 }, 21 22 //獲取用戶消息推送授權 23 getAuthorization(){ 24 wx.requestSubscribeMessage({ 25 tmplIds: ["nITTI4zOSJy3C7EtabjhYjWNkf9OrZ9p5cJQaOpNPYw"], //所選模板的模板ID,可以有多個 26 }).then(res=>{ 27 console.log("獲取消息推送授權成功",res); 28 }).catch(err=>{ 29 console.log("獲取消息推送授權失敗",err); 30 }) 31 }, 32 //獲取用戶輸入的內容 33 getName(e){ 34 console.log("用戶輸入的商品名稱",e); 35 name = e.detail.value 36 console.log(name); 37 }, 38 getTime(e){ 39 console.log("用戶輸入的秒殺時間",e); 40 time = e.detail.value 41 console.log(time); 42 }, 43 getRemark(e){ 44 console.log("用戶輸入的溫馨提示",e); 45 remark = e.detail.value 46 console.log(remark); 47 }, 48 49 //發送訂閱消息給單個用戶 50 sendMsgToOne(){ 51 if(name==null||name==''){ 52 wx.showToast({ 53 title: '請輸入商品名稱', 54 icon:"none" 55 }) 56 return 57 } 58 this.sendMsg(openid,name,time,remark); //調用自封裝方法發送消息 59 }, 60 61 //發送訂閱消息給多個用戶 62 sendMsgToAll(){ 63 if(name==null||name==''){ 64 wx.showToast({ 65 title: '請輸入商品名稱', 66 icon:"none" 67 }) 68 return 69 } 70 let users = ["oWzSH4jwGKmvNajQXjmfGa4j8XF8","otvxTs4dckWG7imySrJd6jSi0CWE"] //兩個openid的示例 71 users.forEach(openid=>{ 72 console.log("多個用戶",openid); 73 this.sendMsg(openid,name,time,remark) 74 }) 75 }, 76 77 //封裝方法,發送商品秒殺的訂閱消息 78 sendMsg(openid,name,time,remark){ 79 wx.cloud.callFunction({ 80 name:"sendMessage", //調用發送消息的雲函數 81 data:{ 82 openid:openid , //openid是要傳給雲函數的參數 83 name:name, 84 time:time, 85 remark:remark 86 } 87 }).then(res=>{ 88 console.log("發送消息成功",res); 89 }).catch(err=>{ 90 console.log("發送消息失敗",err); 91 }) 92 } 93 })
六、效果展示