This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

preface

Now it is necessary to send the notification of duty reminder and inspection reminder to the user through the public account.

There are three types of reminders:

  • On duty to remind
  • Factory inspection Reminder
  • High pressure area inspection reminder

First, remind template design

1.1 Adding a Template

The premise of adding template must be a certified public number. Currently available in ads & Services -> Template messages. Here’s what I added from the template library.

Then send the template message.

Template message is only used for the public account to send important service notifications to users, and can only be used in service scenarios that meet its requirements, such as credit card swiping notification, notification of successful commodity purchase, etc. Do not support advertising and other marketing messages and all other messages that may cause user harassment.

For rules of use, please note:

  1. For all service numbers, you can see the entry for applying for the template message function in function > Add Function Plug-in. However, only authenticated service numbers can apply for and obtain the permission to use template messages.
  2. You need to select two industries where the public account service is located, and you can change the selected industry once a month.
  3. Select the existing template in the template library of the selected industry to call;
  4. Each account can use 25 templates at a time.
  5. Currently, the maximum number of template messages invoked per account is 100,000 times per day. There is no special limit for a single template. [On November 18, 2014, the interface call frequency was increased from the default 10,000 times per day to 100,000 times per day, which can be viewed in the developer center after MP login]. When the number of fans of the account exceeds 10W/100W/1000W, the daily call limit of template messages will be increased accordingly, and the number marked on the developer center page of MP background of the official account shall prevail.

Find the template that works for you and pass in the template ID. Take this template as an example:

1.2 Sending template Messages

As far as I know, there are two ways to send template messages:

  • Public number openId incoming send template message

    Template messages sent TEMPLATE_ENUM_SEND (" ", "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"),Copy the code
  • The applets’ openId incoming sends template messages. (The premise is to bind the public account and small program through wechat open platform)

    UNIFORM_ENUM_SEND (" unified template messages sent ", "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=%s"),Copy the code

The difference is that the openID of the public account is inconsistent with the OpenID of the small program. If the public account is only used as the auxiliary notification of the small program, only the openID of the small program can be maintained in the database, through which the unified service message can be sent. If you are more careful and want to maintain the public account OpenID, you can use the default sending mode. After trying both, I chose the second option for the sake of extensibility.

1.3 Send code test

1. Obtain the accesstoken of the public account first. The service of the public account must be obtained in the public network, so it is relatively difficult to deploy a service to the public network environment.

2, organize the data, such as the following JSON body to send template messages (private information has been hidden).

{ "token": "token", "data":{ "touser": "touser", "template_id": "template_id", "miniprogram":{ "appid":"appid", "pagepath": } "pages/me/index", "data" : {" first ": {" value" : "tomorrow's inspection and scheduling plan", "color" : "# 130 c0e}", "keyword1" : {" value ":" pingdingshan power station ", "color" : "# 130 c0e"}, "keyword2" : {" value ":" remind on duty ", "color" : "# 130 c0e}", "keyword3" : {" value ":" Xiao ray ", "color" : "#130c0e"}, "remark":{"value":" , "color": "#130c0e" } } } }Copy the code

The final effect is as follows:

Second, remind plan design

Based on the notification template, you need to make a reminder plan, such as notification time and frequency.

Now the preliminary design of duty reminder is to remind the first time at 8 o ‘clock in the evening, and remind the second time at 8 o ‘clock in the morning. If the same message more than twice a day, I personally find it distasteful.

In order to expand, we should design a notification table for public numbers, send notifications in it, add two times of inspection time, and then go to the database every hour in the code to find whether the value obtained is in this time end, such as 7.00-8.00 minutes there is no notification task in this time end; If yes, send it and check whether the switch at time 2 is on. If yes, check whether the switch at time 2 is in the current time range and then send it. Both time 1 and time 2 must be the hour. Of course, it might only need to be sent once, so just turn off time 2.

When time 1 arrives, query the user who will be on duty tomorrow and send a message.

When time 2 arrives, query the user who will be on duty today and send a message.

The design of the database table is as follows:

CREATE TABLE `mp_notification_time` ( `id` bigint NOT NULL AUTO_INCREMENT, 'type' varchar(60) COLLATE UTF8MB4_unicode_ci DEFAULT NULL COMMENT 'Specifies the template type. For example: 101 stands for duty reminder; 201 represents factory inspection reminder; Sys_id bigint DEFAULT NULL COMMENT 'power station id', 'temp_id' varchar(120) COLLATE UTf8MB4_unicode_ci DEFAULT NULL COMMENT 'template id', 'Page' varchar(120) COLLATE UTf8MB4_unicode_ci DEFAULT NULL COMMENT 'minor program ', 'time_one' time DEFAULT NULL COMMENT '时 1', 'time_two' time DEFAULT NULL COMMENT '时 2', 'two_ON' tinyint DEFAULT NULL COMMENT 'Whether time 2 is enabled. 0: on. 1 close ', 'create_time' datetime DEFAULT NULL, 'update_time' datetime DEFAULT NULL, 'delete_flag' tinyint DEFAULT NULL COMMENT '0 normal 1 deleted ', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;Copy the code

The scheduled task is executed every hour. If it is judged that there are inspection tasks within this hour, send them. The method to determine the time can be used as follows:

/** * Check whether the current time is in the range of [startTime, endTime], The time format must be consistent. * * @param nowTime current time * @param startTime startTime * @param endTime endTime * @return * @author jqlin */ public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) { if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) { return true; } Calendar date = Calendar.getInstance(); date.setTime(nowTime); Calendar begin = Calendar.getInstance(); begin.setTime(startTime); Calendar end = Calendar.getInstance(); end.setTime(endTime); if (date.after(begin) && date.before(end)) { return true; } else { return false; }}Copy the code

The final effect is as follows: