How to send BuddyBoss Push notifications with OneSignal to a custom PWA?

256 Views Asked by At

I'm currently working on a Social Platform, which is hosted on wordpress with the BuddyBoss theme and plugins. The community is working fine and I also want to launch it as an app. I would really like to have Push notifications in this app, so that if a user writes another user a private message with the buddyBoss messaging tool, the other user will get a push notification for that message. I already have an app which is like a really simple PWA, which I made on a website, where you can just convert your site to an app. The app also has a oneSignal integration, so it is linked to my account, which already works, so I can send a Push notification to all users, which works fine. Now I also want to have Push notifications for private messages.

I tried adding some code to the BuddyBoss theme functions.php, which extracts the OneSignal ID of the user and sends a Push notification to that user with the oneSignal API.

In the following, there is the code I added:

function notify_private_message( $message ) {
    // Get the recipient user ID
    $recipient_id = $message->recipients[0];

    // Get the recipient's OneSignal device ID from user meta data
    $device_id = get_user_meta( $recipient_id, 'onesignal_user_id', true );
    error_log( 'notify_private_message called with device ID: ' . $device_id );

    // Send a custom push notification to the recipient's device
    if ( $device_id ) {
        $content = array(
            'en' => 'You have a new private message'
        );

        $fields = array(
            'app_id' => 'myAppID',
            'include_player_ids' => array( $device_id ),
            'data' => array(
                'bp_message_id' => $message->id
            ),
            'contents' => $content
        );

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications' );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Authorization: Basic mySecretKey'
        ) );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        $response = curl_exec( $ch );
        curl_close( $ch );
    }
}
add_action( 'messages_message_after_save', 'notify_private_message', 10, 1 );

Also with this code I still dont have the Push notification functionality. In my original code I have included my ID and key.

I have tried to add different versions of this code, but nothing worked. I checked if the connection with my App and OneSignal works which it does and I think the error has to be at the connection between BuddyBoss and sending the Message with the right user ID to OneSignal. I also tried different devices which didnt make any difference and additionally I cant see any outgoing messages from my OneSignal Dashboard so that the error should have been somewhere earlier. Maybe it is important to add that I am only doing this for an Android App.

If anyone has an idea how to solve this issue I would be very glad. Best regards, Benjamin

1

There are 1 best solutions below

0
On

I think this can't be done without the change core code of the buddyboss plugin. Because buddyboss sells a mobile application that has the ability to receive notifications from the platform (what you need). That's why I think they blocked this function for ather app