Configure OneSignal for Push Notification

Modified on Sun, 07 May 2023 at 03:58 AM

1. Make sure you have OneSignal Account, if you don't have it please sign up here.


2. Install OneSignal plugin in your WordPress site.




3. Once you installed the plugin you need to setup OneSignal into your WordPress site first.



Setup OneSignal Web Push


1. Go to OneSignal Dashboard.


2. Then click button New App/Website.



3. Set your app name and select the platform of your website (Web -> WordPress) and then configure it.





4. Save it, then you will get the IDs and API Key.



5. Go to your OneSignal Settings in your WordPress site and paste it.



6. Make sure you uncheck the Send notifications additionally to iOS & Android platforms on Sent Notification Settings.



7. Go to your web hosting and login into your control panel.


8. Create the folder mu-plugins inside wp-content.


9. Create file with name onesignal-mobile-support.php inside mu-plugins folder and paste the content of the file with this code.


<?php
    add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);
    function onesignal_send_notification_filter($fields, $new_status, $old_status, $post) {
        /* Goal: We don't want to modify the original $fields array, because we want the original web push notification to go out unmodified. However, we want to send an additional notification to Android and iOS devices with an additionalData property.
         *     */
        $fields_dup = $fields;
        $fields_dup['isAndroid'] = true;
        $fields_dup['isIos'] = true;
        $fields_dup['isAnyWeb'] = true;
        // $fields_dup['android_channel_id'] = "<CHANNEL ID UUID HERE>";
        $fields_dup['data'] = array("post_id" => $post->ID);
        /* Important to set web_url to support opening through both mobile and browser*/
        $fields_dup['web_url'] = $fields_dup['url'];
        /* Important to unset the URL to prevent opening the browser when the notification is clicked for mobile app users */
        unset($fields_dup['url']);
        $onesignal_post_url = "https://onesignal.com/api/v1/notifications";
        /* Hopefully OneSignal::get_onesignal_settings(); can be called outside of the plugin */
        $onesignal_wp_settings = OneSignal::get_onesignal_settings();
        $onesignal_auth_key = $onesignal_wp_settings['app_rest_api_key'];
        $request = array("headers" => array("content-type" => "application/json;charset=utf-8", "Authorization" => "Basic " . $onesignal_auth_key), "body" => json_encode($fields_dup), "timeout" => 60);
        $response = wp_remote_post($onesignal_post_url, $request);
        if (is_wp_error($response) || !is_array($response) || !isset($response['body'])) {
            $status = $response->get_error_code();
            $error_message = $response->get_error_message();
            error_log("There was a " . $status . " error returned from OneSignal when sending to mobile users: " . $error_message);
            return;
        }
        return $fields;
    }


10. Save it, then it will look like this.




11. Your web push notification is already set-up, and you can create notification when you publish a new post. But it's not done yet, you need one more step to configure your mobile app push notification.



Setup OneSignal Mobile Push


1. Go to OneSignal Dashboard.


2. Select your current project/app.


3. Then, go to settings and activate the Google Android (FCM).



4. For this step, you need to add your Firebase Key and Firebase Sender ID. To get this key, you need to create new Firebase account or go to your existing account then create new project.







5. When your project is ready. Then you need to go to the Project's settings.



6. Then click Cloud Messaging menu, and you will see the Cloud Messaging API (Legacy) section. You need to activate it by clicking the there dots icon and click Manage API in Google Cloud Console. This will open a new tab called Google Cloud Console.




7. In Google Cloud Console, click the enable button to activate it.



8. After successfully enabling it, go back to your Firebase project tab and refresh it.


9. After you refresh the Firebase project tab, you will see the new generated keys for your OneSignal requirement. Copy the keys and paste it to the corresponding form in your OneSignal tab.



10. After pasting the keys, click save and continue the process.



11. On the next step, select Flutter as the target SDK then click save & continue.



12. After the process is completed. You will get the App ID for your app. Copy the app ID.



13. Open project folder of your app with Android Studio , then go to lib\main.dart. Replace existing app ID with your app ID.



14. Finally, run the app to register your device to OneSignal. Now you can send your notification by publishing new post on your WordPress site.


Note:

For more information about OneSignal Integration with Flutter you can read the Official Documentation by clicking this link.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article