I talked about the implementation of applets’ notification push in the cloud Development Basics course, and I’ll review it later. But sometimes what if we want to implement the function of timing push
First, the sending of ordinary subscription messages
Let’s take a look at the official synopsis of the subscription message.Next, we will use cloud development to quickly realize the function of small program message push.
1-1, obtain the template ID
This step is the same as our previous template notification push, which is to add the template first, and then get the template IDThe first is to enable the subscription message function, very simple, as shown below
Due to the long-term nature of subscription messaging, it is only open to offline public services such as government affairs and people’s livelihood, medical care, transportation, finance and education, and will gradually support other offline public service businesses in the future. For offline public service alone, long-term subscription messaging is out of reach for most developers. So we can only use one-time subscription messages as an example.As shown above, we select a one-time subscription template from the public template library. Then edit the template as shown belowThe following is the template we have added, and the template ID below is the one we need.
1-2, request user authorization
When we do subscription message authorization, the authorization popup can only be activated after the user clicks or the payment is completed. The official requirements are as follows:Here we use to wx. RequestSubscribeMessage this method, to obtain the authorization of the user.
-
1, write the index. WXML code
-
2. Write the index.js code to achieve click to obtain authorizationThe string of characters in this tmplIds step is the template ID we added ourselves
-
3. Click the button to run the following effect:The authorization popover on your phone looks like this:As you can see, this is the template for the ‘Class reminder’ we added. Careful students can see, the real machine more than a ‘always keep the above choice, no longer ask’ in fact, you carefully more product. You can also understand that when we normally subscribe to message authorization, you can only push a message once if the user allows it. That is, once the user permits, we can push a message to the user, and this permission does not expire. So we can get the user to click as much as possible, so we can send as many messages to the user as possible.
With the user’s permission, we can push messages to users. Next, we will use the cloud function of cloud development to achieve message push function.
1-3, get the opneID of the user
First, let’s see what the official dad has to say.As you can see, there are two official ways to do this. We are using the cloud call here. Basically, call push function in cloud function.
- Push required parametersIt can be seen that I use the OpenAPI function here, and I need to use the user’s OpneID. I have written articles and recorded videos about obtaining the OpenID before. For the article, you can go to my history article, video, click on this:”Obtaining User OpenID with Cloud Function”Here I will not elaborate on the openID acquisition, the corresponding cloud function code posted to everyone.There are a few things to be aware of when using cloud development
- 1. The cloud function directory needs to be created in project.config.json as shown below
- 2. You need to initialize the cloud development environment in app.jsAs for where to get the environment ID of cloud development, I have also talked about many times in the video, directly go to see my video or look at my historical articles.Video on Cloud Development with Zero Basics
1-4. Implement message push with cloud function
We just need to create a cloud function as follows, and then fill in the user’s OpenID, to jump to the applet page link, template content, template ID. Normally this data should be passed in, so for simplicity I’ll write the template content here as fixed.
Note: When I wrote the code above, the key of the push content must be the same as the key in the applet template, otherwise I will report the following error.
- And then look where we call this cloud functionIf the user is not authorized, we will report the following errorIf the user has been authorized, we can push it successfully. The printed log after push is as followsRemember the authorization on our real phone? If the user just clicks permit and does not choose to allow all the time, we will need to re-authorize the user if we push it again after a successful push. Otherwise, the error will still be reportedSo if our users click permit once, we can push a message once. For example, if I click permit four times, I can successfully push four times
rendering
It can be seen that we have successfully received the template message of class reminder, and click it, which is our specific push contentIn fact, I received 4 messages in a row, because I clicked 4 times to allow push, so I can successfully push 4 times.
Here we have a complete implementation of the template message push function, below I put the main code posted to you, you can also private message ME to obtain the complete source code.
- index.wxml
<button bindtap="shouquan" type='primary'> </button> <button bindtap="getOpenid">Copy the code
- index.js
/ / programming small stone wechat: 2501902696 Page ({/ / access authorization click event shouquan () {wx. RequestSubscribeMessage ({tmplIds: [' cfeswarQLMPypjwmiy6AV4EB-izcipu48v8bflkbztu '], // fill in our generated template id success(res) {console.log(' license succeeded ', res)}, Openid getOpenid() {wx.cloud.callfunction ({name: openId getOpenid() {wx.cloud.callfunction ({name: "Getopenid"}). Then (res => {let openid = res.result.openid console.log(" obtain openID successfully ", Openid) this.send(openID)}). Catch (res => {console.log(" failed to get openID ", res)})}, Openid send(openID) {wx.cloud.callFunction({name: "sendMsg", data: {openid: Openid}}). Then (res = > {the console. The log (" push message successfully, "res)}). The catch (res = > {the console. The log (" failure" push message, res)})}})Copy the code
- Push the corresponding cloud function
// Programming small stone wechat: 2501902696 const cloud = require('wx-server-sdk') cloud.init() exports.main = async(event, The context) = > {try {const result = await cloud. Openapi. SubscribeMessage. Send ({touser: event. The openid, / / have pushed to the user page: Thing1: {value: 'miniapp'}, thing6: {value: 'Miniapp'}, thing7: {value: 'templateId'}}, templateId: 'cfeswarqlMPypjwMIy6AV4EB-izcipu48v8bflKbztu' // Template ID}) console.log(result) return result} catch (err) { console.log(err) return err } }Copy the code
Later I will share more knowledge related to small programs, please continue to pay attention.
Note: Only one message can be sent at a time.
Two, send messages regularly
The above user authorization and sending messages need to be manually clicked to realize sending. But sometimes we need to regularly remind the user, such as the alarm clock small program, to regularly remind the user, how to do it, next we will realize the function of regularly sending messages.
- Pay attention to
Of course, in this case, you still need to authorize to send a message, which is also authorized to send a message at a time, so you need to authorize as many times as possible
2-1, what is a timing trigger
The timing trigger in the cloud function is used to realize the function of timing sending. The official introduction is as follows.You can read it for yourself if you have timeDevelopers.weixin.qq.com/miniprogram… The authorities already taught us how to write a timing trigger
2-2, timing trigger time setting rules
I suggest you read the official documentation carefully.Here are some official examplesHere I use the time trigger to call our cloud function every 5 seconds to send the subscription message.
2-3, add a timing trigger
As shown in the figure below, we need to create a new cloud function timerWe need to call our Fasong cloud function in the Timer cloud function to implement the send function
Then create a new config.json file under the Timer folder Then do the following configuration for config.jsonNote that there can be no comments in json. The configured triggers are as follows
2-4. Deploy the timing trigger
Once added, remember to deploy the trigger
2-5, timing send effect
The first step is to see if the trigger executes every 5 secondsThen check to see if the phone has received a messageYou can see we’re getting messages on our phones every five seconds. Again, remember that multiple permissions allow multiple messages.
Of course, we can’t send messages to customers every 5 seconds like this. This will annoy customers and it will be easy to block, so we can stop the trigger
2-6, stop the trigger
Here we have also realized the function of sending messages at a scheduled time. Of course, to send messages to the specified users, we need to obtain the user OpenID first and let the users have multiple authorization.