1

I am using Push Notifications module to send notification to registered tokens.

I am able to send a plain text notification successfully via rules.

I am looking for a way to send the notification with extra fields so that it can be identified which type of notification is this and what action to take on this.

Below is the data example I need to send the notification in:

{
   "aps":{
      "alert": "Enter your message",
      "badge":1,
      "sound":"default",

     "extraField 1":"some value",
     "extraField2":"identifier"
   }
}

Here is the rule export:

    { 
    "rules_push_notification" : {
    "LABEL" : "Push Notification",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "push_notifications" ],
    "ON" : { "node_insert" : [] },
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "music" : "music", "news" : "news" } }
        }
      }
    ],
    "DO" : [
      { "push_notifications_send_message_bulk" : { "message" : "[node:title]", "target_group" : "authenticated" } }
    ]
  }
}

Notes:

  • My Rules Action maps (corresponds) to the data example like so: that "Enter your message" would be replaced by the value of "[node:title]".
  • About that "extra field 1" (and "extra field 2"): When I send a Push notification, On Tap it needs some action to be taken which is decided in those extra fields, for an example if I need to show a music article I will send "music" as identifier and second field will be the node id of the content help mobile to open that particular article.
1
  • Thanks for the text suggestion, I have tried that earlier. On iOS whatever is sent in the notification object "alert" key appears same in the notification for an example, it will appear {1234 (nid)} / {music (node type)} / The title (The title of the node) which does not looks good actually.
    – Puneet
    Apr 27, 2017 at 13:05

3 Answers 3

0

The current D7 release of the Push Notifications module does not allow/support such extra fields (as in your question). However below are some suggestions to get you closer ...

Integrate 'identifier' in your Alert message

You could produce 2 types of alert messages, based on the content type (= News or Music), by adapting your Rules Action. This by also integrating the available [node:content-type] token, it could look something like so:

      { "push_notifications_send_message_bulk" :
        { "message" : "[node:content-type] notification: [node:title]",
          "target_group" : "authenticated" } }

Using this approach, your message starts to look like a kind of transaction to be processed, for which there might be solutions to further process it on your target platform).

Integrate 'some value' in your Alert message

Using a similar approach, you could further extend this to also include that "some value" related to your "extraField 1" (as in your question), probably you'd want to use the available [node:nid] token for that.

However, as per your comment (="it will appear {1234 (nid)} / {music (node type)} / The title (The title of the node) which does not looks good actually."), you may want to try another approach (as explained in the next suggestion).

Send JSON messages

Your data example seems to be a variation of what's shown in the issue about "sending JSON messages", in which the notification message is a JSON string that looks like so:

{"title": "my title",
 "message": "my message",
 "additionalData": { "key": "value", ... }}

Comment #3 in that issue refers to the issue about "Custom Payloads for Push Notifications", and which includes a patch (current status = needs review) with which you might get it to work somehow.

Upcoming improvements in D8

Help is on its way, as detailed in the D8 related issue about "Create content entity for push notifications".

2
  • Thanks for the efforts, On "sending JSON messages" Suggested solution link is not available now, Could you help me there please?
    – Puneet
    Apr 28, 2017 at 3:50
  • @Puneee you're right, that's a broken link. And which is also why I continued digging via the next comment in that same issue, to find out what that "custom payload" thing (in comment nr 3 of it) referred to. It turned out to be the "comment #3" I then linked to in my answer above. Apr 28, 2017 at 13:23
0

I am already facing same problem before 2 years back. Which I resolved thrugh using of custom php code in rules action and passing code something like below.

$uids = array('1');
$message = array(
             'alert'=>'Enter your message',
             'badge'=>1,
             'sound'=>'default',
             'extraField1'=>'some value',
             'extraField2'=>'identifier'
           );
push_notifications_send_message($uids,$message);

and applying patch which is attached herewith. Patch is compose and uploaded just for your issue.

5
  • Can I make that in a hook or something as changes in module directly will remove the code (Patch) at the time of module update. Or if you could suggest something else Please?
    – Puneet
    Apr 28, 2017 at 8:18
  • create custom module and use whole function push_notifications_send_message in it.
    – vgoradiya
    Apr 28, 2017 at 11:14
  • Thanks for your help. Added a cusotm module function, Please make required corection to your answer so that I can accept the answer.
    – Puneet
    Apr 28, 2017 at 13:10
  • @Puneee : instead of your edit suggestion from a few mins ago, I suggest you post your OWN answer, with something like "starting from the answer posted by vgoradiya, I was able to solve my question via an additional improvement which looks like so and so". That way you have an answer to your own question, without trying to change this answer here via your suggested edit, which IMO is not at all compliant to the rules of this site. Good luck! Apr 28, 2017 at 13:26
  • @Puneee Actually your want some suggestion create custom module instead of applying patch. Otherwise applying patch and using custom php code in rules working for any one. When patch once reviewed it would be part of module and you can be update it.
    – vgoradiya
    May 1, 2017 at 13:33
0

Starting from the answer given by vgordiya, I was able to solve the problem.

I created my own custom module with dependencies[] = push_notifications.

In my rule, I have added the custom PHP code in action and passing code something like below.

$uids = array('1');
$message = array(
             'alert'=>'Enter your message',
             'badge'=>1,
             'sound'=>'default',
             'extraField1'=>'some value',
             'extraField2'=>'identifier'
           );
cusotm_module_send_message($uids,$message);

My custom module function I added is:

function cusotm_module_send_message($recipients, $message) {
    if (!is_array($recipients) || !is_array($message)) {
        return FALSE;
    }

    if (!module_exists('push_notifications')) {
        watchdog('cusotm_module', t('Push Notification Module not found'));
        return false;
    }

    // Shorten the message characters / 8 bit.
    $message = truncate_utf8($message, PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT, TRUE, TRUE);

    // Convert the payload into the correct format for delivery.
    $payload = $message;

    // Determine if any of the recipients have one or multiple tokens stored.
    $tokens = array();
    foreach ($recipients as $uid) {
        $user_tokens = push_notification_get_user_tokens($uid);
        if (!empty($user_tokens)) {
          $tokens = array_merge($tokens, $user_tokens);
        }
    }

    // Stop right here if none of these users have any tokens.
    if (empty($tokens)) {
        return FALSE;
    }

    // Send a simple alert message.
    push_notifications_send_alert($tokens, $payload);
}

The reason behind doing it this way was to not changing the original module code so that the patch don't get vanished at the time of update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.