• Resolved psytanium

    (@psytanium)


    Hi,

    Im using a webview app, to open the received notifications with the App instead of browser, i should insert in Additional Data :

    – Key : url
    – Value : the post url

    But using this wordpress plugin, i cannot see where is the Additional Data.

    Anyway to add those fields automatically or from the settings ?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter psytanium

    (@psytanium)

    Hello,

    Any help please ? The app the prepared to receive deep linking notifications, but using the Additional Data fields.

    Because the plugin is not the same as OneSignal dashboard, its missing the Additional Data, I need small help to make :

    1 – Additional data[Key] = “URL”
    2 – Additional data[value] = Initial launch URL

    ————–

    Im using the code provided in the documentation :

    <?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[‘isWP’] = false;
    $fields_dup[‘isAdm’] = false;
    $fields_dup[‘isChrome’] = false;
    // $fields_dup[‘android_channel_id’] = “<CHANNEL ID UUID HERE>”;
    $fields_dup[‘data’] = array(“myappurl” => $fields[‘url’]);
    /* 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&#8221;;
    /* 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;
    }

    ?>

    Thread Starter psytanium

    (@psytanium)

    More explanation in image : https://imgur.com/a/IDVWnnA

    Hi. You can follow the guide here on customizing the notification payload: https://documentation.onesignal.com/docs/web-push-wordpress-faq#method-2-deep-linking

    In this case you will want to set the $fields['data'] value, like so:

    $fields_dup['data'] = array("url" => $fields['url']);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Include Additional Data in notifiactions’ is closed to new replies.