Comments

haagendazs’s picture

Component: Miscellaneous » Code
Assigned: Unassigned » haagendazs

Hi there. Integrating this module with Rules should be relatively simple, but I'm somewhat unsure of what the desired behavior is. Do you want to send a mass-push notification when a certain action happens, or are you planning to send individual push notifications?

mem-tn’s picture

Hi,
I also think that this is an important feature, it is important to be able to send a message when something happend on the server.
can you please implement it.

+1

facine’s picture

+1 to send individual push notifications.

Thanks!!

serkanb’s picture

Status: Active » Needs review
StatusFileSize
new1.16 KB

I just needed the 'send a notification to this users on this event'-Action.

Takes a list of integers/uids and a message,
calls push_notifications_send_message with the parameters.

In case you only want to notify one user, you can give one uid as parameter and it should be fine.

kenwen’s picture

Agree that this would be a really useful feature

As a use case I would want users to be notified when a certain node type has been published and to open an app

jca’s picture

Title: Rules? » Add support for Rules module
Version: 7.x-1.0-alpha1 » 7.x-1.x-dev
Assigned: haagendazs » Unassigned
Issue summary: View changes
StatusFileSize
new11.69 KB

Implements integration between rules and push_notifications. The following events are given with this patch:

  • push_notifications_before_token_insert: Before a token of a new device is inserted
  • push_notifications_after_token_insert: After a token of a new device is inserted
  • push_notifications_before_token_delete: Before a token of a new device is deleted
  • push_notifications_after_token_delete: After a token of a new device is deleted
  • push_notifications_after_apns_feedback: After the feedback of APNS
  • push_notifications_after_apns_send: After send a push_notifications to iPhone/iPad using APNS
  • push_notifications_after_c2dm_send: After send a push_notifications to Android using C2DM
  • push_notifications_after_gcm_send: After send a push_notifications to Android using GCM
penyaskito’s picture

Issue tags: +#D8SVQ, +#SprintWeekend2014

Tagging with the sprint tags.

jlbellido’s picture

Issue tags: -#D8SVQ, -#SprintWeekend2014 +D8SVQ, +SprintWeekend2014

Changed tags

scotthooker’s picture

Good patch.

scotthooker’s picture

Status: Needs review » Reviewed & tested by the community
haagendazs’s picture

Looks great, guys! Please give me a few more days to merge this and create a new release.
Excited that this feature will be available thanks to your patch!

scotthooker’s picture

I haven't tested on all platforms only ios.

Bevan’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new548 bytes

Here is another rules integration.

haagendazs’s picture

Reviewed the different patches. It looks like people have different objectives for an integration with Rules: creating actions (SerkanB, Bevan) vs. creating events (jca).

@SerkanB @Bevan: For creating a simple action, SerkanB's patch looks pretty good and accomplishes the action of sending out a push notification to specific users. @Bevan: the push_notifications_send_message function expects user ids as opposed to user objects. I'm attaching a valid git patch that's almost identical to SerkanB's patch.

@jca: Do you have an actual use case for the number of rules events you created in your patch? I'm asking because it looks like you're trying to access very specific events (e.g. before token gets inserted, after token gets inserted) that seem to be targeted towards developers rather than site builders. Do you think exposing a custom hook for these actions might be more appropriate? Let me know either way and I'll wrap your changes into a single patch, including the rules action.

scotthooker’s picture

Even if they are targeted towards site builders having lots of events in rules generally is a good thing. We rely on the a lot in Commerce.

haagendazs’s picture

Status: Needs work » Needs review
StatusFileSize
new12.88 KB

Here's a new patch that combines #4 and #6. This one should be patched against the latest 7.x-1.x-dev version as there were a bunch of modifications since #6 was created. I modified the wording for the rules events slighty to get them in line with the standard rules wording, the functionality from #6 should be untouched.

This patch should includes the following:

  • Provides a rules action (Send a push notification) that sends out a push notification to users with registered device tokens
  • Provides a number of rules events to allow other modules to react to the following events:
    • After deleting a device token (push_notifications_after_token_delete)
    • After saving a new device token (push_notifications_after_token_insert)
    • After sending an Android push notification (C2DM) (push_notifications_after_c2dm_send)
    • After sending an Android push notification (GCM) (push_notifications_after_gcm_send)
    • After sending an iOS push notification (APNS) (push_notifications_after_apns_send)
    • Before deleting a device token (push_notifications_before_token_delete)
    • Before saving a new device token (push_notifications_before_token_insert)
    • iOS feedback service was completed (push_notifications_after_apns_feedback)

  • Commit c857a91 on 7.x-1.x by haagendazs:
    Issue #1658132 by jca, SerkanB, haagendazs: Add Rules integration....
haagendazs’s picture

Committed changes to 7.x-1.x-dev

dromansab’s picture

Hi,

I'm registering token with anonymous user. How can I send a mass message to al registered tokens?

Thanks!

redorbit’s picture

Maybe a little bit late, but that`s how we send PN`s to all (anonymous) registered users based on rules events.

We are just reusing the function of the module, when a message is sent through the admin interface at "admin/config/services/push_notifications/message"

Example for IOS recipients:

1. Setup a rules event, e.g. content update
2. As action use "Custom PHP Code" and paste in the following code (payload is the notification text):

########## Action for IOS
########## Use action "Execute custom PHP code"

    $payload = "News Available";
    $language = false;
	$tokens_ios = push_notifications_get_tokens(PUSH_NOTIFICATIONS_TYPE_ID_IOS, $language);
    if (!empty($tokens_ios)) {
      // Convert the payload into the correct format for APNS.
      $payload_apns = array('aps' => $payload);
      $result = push_notifications_apns_send_message($tokens_ios, $payload_apns);
      $dsm_type = ($result['success']) ? 'status' : 'error';
      drupal_set_message($result['message'], $dsm_type);
    }
    else {
      drupal_set_message(t('No iOS recipients found, potentially for this language.'));
    }

Example for Android recipients:

1. Setup the same event, e.g. content update
2. As action use "Custom PHP Code" and paste the following code:

########## Action for ANDROID
########## Use action "Execute custom PHP code"

    $payload = "News Available";
    $language = false;
	$tokens_android = push_notifications_get_tokens(PUSH_NOTIFICATIONS_TYPE_ID_ANDROID, $language);
    if (!empty($tokens_android)) {
      // Determine which method to use for Google push notifications.
      switch (PUSH_NOTIFICATIONS_GOOGLE_TYPE) {
        case PUSH_NOTIFICATIONS_GOOGLE_TYPE_C2DM:
        $result = push_notifications_c2dm_send_message($tokens_android, $payload);
        break;

        case PUSH_NOTIFICATIONS_GOOGLE_TYPE_GCM:
        $result = push_notifications_gcm_send_message($tokens_android, $payload);
        break;
      }
      $dsm_type = ($result['success']) ? 'status' : 'error';
      drupal_set_message($result['message'], $dsm_type);
    }
    else {
      drupal_set_message(t('No Android recipients found, potentially for this language.'));
    }

A little bit annoying to setup the same rule two times and it`s also kind of a dirty hack, but it works.

ryandekker’s picture

Since this is already committed, I added a patch with a bit more flexibility in this issue. Would love some feedback over there. #2410077-1: PN Rules - sending to users of a role and dynamic selection of recipient

RyJ’s picture

Hi,

Thanks for the patch!

Once applied, I used the code for android of comment #20 on notification payload, and 1 in array of uids.

Everything works fine, but in the notification received in the status bar of the device only the first letter of $payload is shown.

Why?

Again, thanks.

RyJ’s picture

Fixed issue reported in the previous comment.

Make the following change: $payload = "News Available"; to $payload = array( 'alert' => $node->title);.

ptmkenny’s picture

Category: Support request » Feature request
anujdeo’s picture

@redorbit:

First of all, Thanks a lot for your custom PHP code. It works and helped me:)

Just a little query.. Should $payload be an array?

Because when I tried your code- It was sending out pushes to registered devices but it was sending only first character of whatever in $payload variable.
It was also throwing waring at line 749 of push_notifications.module : "Invalid Arguments passed to foreach()"

So, I changed this line:
$payload = "News Available";

To

$payload = array(
    'alert' => 'Data Updated'
);

And it works perfectly fine :)

Thanks again...

Please Note : I am very new to Drupal & PHP. So correct me iif I am wrong in above solution.

  • haagendazs committed 9b2d2a6 on 7.x-1.x
    Issue #1658132 by mihai_brb: Create token list for admin page
    
haagendazs’s picture

Version: 7.x-1.x-dev » 7.x-1.2-beta1
Derimagia’s picture

Status: Needs review » Needs work

The actions here are pretty wacky. The action for sending push notification requires an array of user ids. Why can't you send it to an individual user? Why isn't it just an account or list of accounts? Right now you can't even do something like "On user login send push notification" since you need an array of ids

serkanb’s picture

It's an array of integers because that's what push_notifications_send_message expects it to be.

As for the "I just want to notify a single use", see #4:

In case you only want to notify one user, you can give one uid as parameter and it should be fine.

The event for a user login isn't really an issue of this module.

I think (without testing or looking which events exist) it should be pretty easy to create a rule which notifies an user if a user just logged in.

Where exactly did your attempt fail?

Derimagia’s picture

Sorry, let me rephrase - it didn't fail it just doesn't use it's functionality currently.

The point of rules it so pass data types back and forth and to act on them not to have to make separate variables in rules for every uid you want to set a uid too.

"push_notifications_send_message" expecting uids is not a reason to not implement a function to map them like a user would expect. If this isn't for a reason I'll make a function to enhance this, but the fact that it wasn't done in the first place makes this very weird especially since it only takes a few minutes to do.

Derimagia’s picture

The fact that push_notifications_send_message accepts a user id list is also very strange but that's obviously not related to this issue unless we were to just make that function be better at what it accepts

haagendazs’s picture

Status: Needs work » Needs review

@Derimagia, @SerkanB: Thanks for the comments about the Rules integration. TBH: I'm not an avid Rules user, so any feedback is welcome. And very helpful.

Derimagia, you were right. The previous action was working, but very limited to specifying an array of individual user ids. Originally, I wanted to use the push_notifications_send_message function in order to prevent creating another function, but that wasn't a good call.

I followed your suggestion and changed the action to send a message to only a single user identified by the account. Could you please review the change in the dev branch and see if this works better (see http://cgit.drupalcode.org/push_notifications/commit/?id=58a5001)? Using this change, I was able to create your example rule.

Thanks for testing.

Derimagia’s picture

I'm not a massive rules user as well but I was interested in making this work since I sometimes use rules for emails / notifications and things like that.

I ended up doing some more work on this - specific related to payloads. This may belong in it's own thread but I'll share what I did so far. It's half related to rules and half not.

  • Fixed processing of apns payloads
  • Allowed Payloads to be sent in rules, this can probably be moved to the admin ui as well (Uses options_element if it's enabled)
  • Some code refactoring since s lot of the functions for payloads/sending messages were very similar and I needed some custom payloads-
  • Optimizations for loading tokens (Just loaded them all at once instead of per use)

Let me know your thoughts - payloads are probably something most people won't need though. So far it's worked well for me and my mobile team

haagendazs’s picture

@Derimagia This looks awesome at first glance. The custom payload thing is an issue that quite a few people asked for. Could I ask you for a huge favor though: Would you mind creating a new issue for your patch? This thread has been going through so many revisions and different purposes, it's getting really unwieldy. I was hoping to get 7.x-1.2 released with just the current Rules implementation (including the fix from this afternoon).

Derimagia’s picture

Understood and I agree - Moved to Issue https://www.drupal.org/node/2716957

Derimagia’s picture

haagendazs’s picture

Thanks a lot. If you still have a minute, would you mind giving just this change a quick try and change the status if it works? I'd really appreciate it.

Derimagia’s picture

Status: Needs review » Reviewed & tested by the community

LGTM for this thread. Only small things are that it would be nice to not have a lot of functions that do the same thing with slightly different parameters (My point is petty here) and right now it doesn't allow for sending to multiple people in bulk (Push Notifications can bulk send and if you are sending to a lot of people you don't want to do it to 1 account at a time), but if the payload stuff is to be merged in it'll need to be altered anyway. You may also want to leave in the integer sending that was here before (See my patch) depending on how many people you think are using it.

Marking as Reviewed.

haagendazs’s picture

Assigned: Unassigned » haagendazs
Status: Reviewed & tested by the community » Fixed

Thanks for reviewing and the status change. With this latest change, you can still send push notifications to a single account identified by an integer if you switch to "Direct Input Mode" for that specific field in the action. I thought this would be useful if an admin user wants to be notified via push notification when certain actions happen.

I'll mark this issue as closed and will look at your separate issue shortly.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.