How to use Rules to replace eMails by iOS-like notifications?


Consider some custom rule, created with the Rules module, which performs some Rules Action to send eMails to multiple eMail Ids (i.e. 1 eMail to each individual user).

As an example, consider this rule (in Rules export format), included in an answer to the question about "How to notify friends about new content?":

    { "rules_notify_friends_about_new_content" : {
        "LABEL" : "Notify friends about new content",
        "PLUGIN" : "reaction rule",
        "OWNER" : "rules",
        "REQUIRES" : [ "rules" ],
        "ON" : { "node_insert" : [] },
        "DO" : [
          { "VIEW LOOP" : {
              "VIEW" : "my_friends",
              "DISPLAY" : "views_rules_1",
              "ROW VARIABLES" : {
                "user_name" : { "user_name" : "User Name" },
                "user_id" : { "user_id" : "User Id" },
                "user_email_id" : { "user_email_id" : "User eMail Id" }
              "DO" : [
                { "drupal_message" : { "message" : "Your friend with user name [user-name:value] (and id = [user-id:value]) will receive an eMail notification about your new node with title \u0022[node:title]\u0022 and node id [node:nid] (delivered to eMail Id  [user-email-id:value])." } },
                { "mail" : {
                    "to" : [ "user-email-id" ],
                    "subject" : "Your friend [node:author] created a new post ...",
                    "message" : "Go checkout the new post from [node:author] titled \u0022[node:title]\u0022, located at [node:url] ",
                    "language" : [ "" ]

How can this rule be improved, to replace the Rules Action that sends those eMails by some type of iOS-like notifications?


1 answer


A possible translation / summary of the original (linked) question is like so:

When a user publishes some new content, I want that for every user shown as a 'friend' (in the View 'All friends'), a Facebook style notification is shown. And this in such a way that (a) the notifications are based on the Message module and (b) for each of those friends such notification can be marked as read by each friend individually.

Below is a blueprint of how you can get that to work, assuming you already implemented the solution explained in the previous answer (which only sends an appropriate eMail to each of the user's friends).

Part 1 - Messages module

Use the Message module to create "Messages" to be shown/delivered to the user's friends. So that you expand the rule (shown in the question here) to have the Rules module create message "entities" (which is what you get if you use the Message module). And then just use the power of Views to display those messages that got created in the format you need (page, block, etc). Here is what you'd need:

  1. Define an appropriate Message type, using the Message module. Think of Message types as similar to "Content types", which will be used to create Messages, similar to Nodes. Iimagine that the message text you'll want to use is similar to the eMail produced by the rule shown in the question here.

  2. Enhance the rule (shown in the question here) with a Rules Action "Create entity" (of type "Message"). Refer to the answer in "Which user related token(s) can be used to create a Rule for setting up a message type?" for some examples.

  3. You probably also want to add a Rules Action to pass any relevant tokens, via Rules, as Replacement tokens that you can use in your Message type you created with the Message module. Refer to What's the purpose of "Replacement tokens" of a Message type? for more details on this.

  4. Use Views to display the created messages in the format you prefer.

Part 2 - Make messages manageable

The Message module is great, but it doesn't really come with some UI where each user can manage (view, mark read, etc) their messages. That why you may want to also perform the steps explained in "How to allow users to manage their own Message Stack messages?", which can be summarized like so:

  1. Create a flag (using the the Flag module).
  2. Create a view 'Messages by user'.
  3. Use Rules to flag/unflag messages.
  4. Trigger the Rules Component to mark messages (using the VBO module).
  5. Create notifications about Unread Messages (using the "Menu Badges" module, further detailed below).

Part 3 - Menu Badges module

The Menu Badges module is all you need to add such notifications, which you can add to any menu item and/or menu tab of your choice.

Here are some details about it (from its project page):

Provides a method for adding iOS-style badges to menu items.

(see screenshot)

enter image description here

Adding Badges to Menus

Once enabled, go to Administration > Structure > Menus, and click "list links" next to the menu containing the target item. Click "edit" next to the item and select the badge to display with the Display Menu Badge select box.

Adding Badges to Tabs

For adding badges to menu tabs, there is a tab under Administration > Structure > Menus called "Tab Menu Badges". Search for the menu router path of the tab you want to alter, then select a badge from the corresponding select box.

Badge Types

The module includes five example badges, and new badges can be created easily with Views. Modules can also supply their own badges with a hook implementation. See the README.txt file for details.

Because of how it integrates with the Views module, you basically reduce the solution for any type of notification to something like "Just create an appropriate view of it".

However, there is a small caveat: if you really want the actual number (shown in the red circle) to be accurate, make sure you have some field in your view that will actually correspond to the "new" part of your question. To better explain this: have a look at the "tracker" view (disabled by default), which has the fields "Content:Has new content" and "Content:Has new comments". Those are 2 perfect fields to use if you want to use Menu Badges to get notified whenever there is new content, or new comments.

Video tutorials:

Fruit of your labor

After you implemented the solution explained in this answer, you have solved the challenge summarized in the beginning of this answer.