WeChat applets enable one-to-multiple message sending

Source: Internet
Author: User
Tags notification center
This article describes how to implement one-to-multiple message sending and related information about the instance code in the applet. For more information, see Implementation of one-to-multiple message sending and instance code in the applet

The passing of values and notifications between interfaces in the applet is quite painful. Therefore, it imitates the Notification Center in iOS and writes a set of similar notification centers in the applet.

The notification center can send one-to-multiple messages and deliver objects. Simple to use.

Register a notification name on the interface for receiving messages. Then, post the notification name on the interface for sending the message. You can register the same notification name on multiple interfaces. In this way, you can send one-to-multiple messages.

Usage:

1: reference notification. js in app. js

Var icationicationcenter = require ('/utils/notification. js'); // change it to your absolute path.

2: Add the following in app. js:

App({  onLaunch: function (){     this.notificationCenter = notificationCenter.center();  },  notificationCenter:null,})

3: Register in page. js that receives the notification

PageA. js:

Var app = getApp (); Page ({onLoad: function (options) {app. icationicationcenter. register ("one notification name", this, "didReceviceAnyNotification") ;}, didReceviceAnyNotification: function (name, content) {console. log ("received notification:", name, content );},})

4: page. js that sends the notification

Arbitrary PageB. js functions

Var app = getApp (); Page ({anyFunction: function () {app. icationicationcenter. post ("notification name", {// any notification object });},})

Implementation:

Http://xiazai.jb51.net/201702/yuanma/wxappNotificationCenter-master (jb51.net).rar

Var icationicationcenter = {icationicationcenter :{}, // register a listener with the notification center. // Name: Listener notification name // observer: Listener // action: method name called when the listener receives the notification, // func: The function called when the listener receives the notification, // action func 2 Select 1 register: function (name, observer, action, func) {if (! Name |! Observer) return; if (! Action &&! Func) return; console. log ("registration notification:", name, observer); var center = this. icationicationcenter; var objects = center [name]; if (! Objects) {objects = [];} this. remove (name, observer); objects. push ({observer: observer, action: action, func: func}); center [name] = objects ;}, // remove a listener from the Notification center remove: function (name, observer) {if (! Name |! Observer) return; var center = this. icationicationcenter; var objects = center [name]; if (! Objects) {return;} var idx; var object; for (idx = 0; idx
 
  

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more articles about how to implement one-to-multiple message sending in small programs, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.