Skip to main content

Push notifications, any news?

Hello!

I am trying to hook on push notifications for the app we created around fc, but can't find anything anywhere in the code?

Is it going to be implemented?

Any suggestion otherwise?

Thank you,

Giorgio

Dakota Snow

I'm interested in this too

hello everyone! I know this is a hot topic for many people, since notifications improve a lot the community interactions.

I've been working on a solution for me and I want to share it, in case anyone else might need it.

This is simply a code snippet (working) that you can add. In the code you can see I eventually forward the notification to wpmobile.app (the plugin I am using to transform my community into an app), but there you can add the code for any other external notification management system.
Here's the snippet, I hope you'll enjoy it :)

add_action('plugins_loaded', function () {

// Helper function to log debug messages
if (!function_exists('fluentcommunity_debug_log')) {
    function fluentcommunity_debug_log($message) {
        if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
        }
        error_log("[FLUENTCOMMUNITY DEBUG] " . $message);
    }
}

// List of FluentCommunity hooks to intercept
$hooks = [
    'fluent_community/space_feed/created',
    'fluent_community/email_notify_new_posts',
    'fluent_community/comment_added',
    'fluent_community/comment_added_async',
    //'fluent_community/email_notify_users_everyone_tag',
    //'fluent_community_send_daily_digest',
    //'fluent_community/space/join_requested',
];

foreach ($hooks as $hook_name) {
    add_action($hook_name, function (...$args) use ($hook_name) {
        fluentcommunity_debug_log("πŸ“Œ Hook triggered: $hook_name");
        // fluentcommunity_debug_log($args);

        $recipient_email = null;
        $user_id = null;

        foreach ($args as $arg) {
            // If argument is an array
            if (is_array($arg)) {
                if (isset($arg['user_email'])) {
                    $recipient_email = $arg['user_email'];
                    break;
                }
                if (isset($arg['user_id'])) {
                    $user_id = $arg['user_id'];
                }
            }

            // If argument is an object and has user-related properties or methods
            if (is_object($arg)) {
                if (method_exists($arg, 'getUserId')) {
                    $user_id = $arg->getUserId();
                } elseif (method_exists($arg, 'getAttribute') && $arg->getAttribute('user_id')) {
                    $user_id = $arg->getAttribute('user_id');
                } elseif (property_exists($arg, 'user_id')) {
                    $user_id = $arg->user_id;
                }
            }
        }

        // Fallback: retrieve email from user ID if needed
        if (!$recipient_email && $user_id) {
            $user = get_userdata($user_id);
            if ($user) {
                $recipient_email = $user->user_email;
                fluentcommunity_debug_log("πŸ“§ Email retrieved from user_id $user_id: $recipient_email");
            } else {
                fluentcommunity_debug_log("❌ User not found with ID $user_id");
            }
        }

        if (!$recipient_email) {
            fluentcommunity_debug_log("❌ No recipient email found for hook $hook_name");
            return;
        }

        // Default notification content
        $title = 'New activity in the community';
        $message = 'Check what’s going on!';
        $link = home_url('/community/');
        $image = '';

        switch ($hook_name) {
            case 'fluent_community/comment_added':
            case 'fluent_community/comment_added_async':
                $comment_attrs = method_exists($args[0], 'getAttributes') ? $args[0]->getAttributes() : (property_exists($args[0], 'attributes') ? $args[0]->attributes : []);
                $post_attrs = method_exists($args[1], 'getAttributes') ? $args[1]->getAttributes() : (property_exists($args[1], 'attributes') ? $args[1]->attributes : []);

                $comment_id = $comment_attrs['id'] ?? null;
                $post_title = $post_attrs['title'] ?? 'your post';
                $comment_text = $comment_attrs['message_rendered'] ?? '';
                $author_id = $comment_attrs['user_id'] ?? null;

                $author_user = $author_id ? get_userdata($author_id) : null;
                $author_name = $author_user ? $author_user->display_name : 'Someone';

                $link = $comment_id ? home_url('/community/comment/' . $comment_id) : home_url('/community/');

                $title = "New comment or reply to \"$post_title\"";
                $message = "$author_name wrote:\n" . strip_tags($comment_text);
                break;

            // case 'fluent_community/email_notify_new_posts':
                // $message = 'A new post has been published.';
                // if (!empty($args[0]['feed_id'])) {
                    // $link = home_url('/community/feed/' . $args[0]['feed_id']);
                // }
                // break;

            case 'fluent_community/space_feed/created':
                if (!empty($args[0])) {
                    $feed = $args[0];

                    $title = 'New post: ' . $feed->title;
                    $message = strip_tags($feed->message_rendered);
                    $link = home_url('/community/feed/' . $feed->id);
                } else {
                    $message = 'A new post has been published in a space you follow.';
                }
                break;

            // case 'fluent_community/email_notify_users_everyone_tag':
                // $message = 'You were mentioned in a post!';
                // break;

            // case 'fluent_community/space/join_requested':
                // $message = 'Someone requested to join a space.';
                // break;

            // case 'fluent_community_send_daily_digest':
                // $message = 'Your daily community digest is ready.';
                // break;
        }

        // Send push notification via WP Mobile App
        if (function_exists('wpmobileapp_push')) {
            wpmobileapp_push($title, $message, $image, $link, 'all', '', $recipient_email, true);
            fluentcommunity_debug_log("βœ… Push sent to $recipient_email with title '$title'");
        } else {
            fluentcommunity_debug_log("❌ wpmobileapp_push function is not available");
        }
    }, 99, 10);
}

});

D@vid

Giorgio LucianiΒ Thx Gorgio !

Giorgio LucianiΒ Hi Giorgio! πŸ™Œ

Great job and thank you for sharing your snippet β€” it’s super helpful to see a working example in the wild. We’re also running FluentCommunity inside a native wrapper and push is the last missing piece on our side.

We’re currently wiring push with OneSignal (external user ID + tags). Your snippet is using wpmobileapp_push(), so a couple of questions to understand the flow better:

  1. Placement – where did you finally place the snippet? As an MU-plugin in wp-content/mu-plugins/, a regular plugin, or in the theme?

  2. Hooks – which FC hooks ended up being the most reliable for you (new post, comment, mention)?

  3. Recipient resolution – in your code you derive user_email / user_id. Is that what wpmobileapp_push() expects to target the device?

  4. Provider – are you sending through WPMobile.App only, or did you also try OneSignal?

    • If you tried OneSignal: did you use external user IDs or player IDs, and do you have a small REST payload example you can share?
  5. Gotchas – any tips from prod use (ordering of hooks, delays, rate limits, caching, etc.)?

Again, thanks a lot for sharing β€” any extra context will help many of us in the community move this forward πŸš€

β€” David

arjun arjun

Ross FCAΒ implementing some kind of push notification would be a great add-on.

Ross FCA

arjun arjunΒ Working on it.

arjun arjun

Ross FCAΒ Great. Do let us know once the progress nears its completion.

Dakota Snow

I just used http://wpmobile.app/ to build an app and did some custom coding to make push notification work and its amazing everyone in my community loves it.. and it all custom white lable with no monthly cost!