• Resolved macbethmanene

    (@macbethmanene)


    Hi

    I uSE BELLOW Code , But it sends to both app and browser, please advise i need only app notification

    <?php

    //Push notify OneSignal Filter

    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.
    */

    /* Not entirely sure if this PHP function makes a deep copy of our $fields array;
    it may not be necessary. */
    $fields_dup = $fields;
    $fields_dup[‘isAndroid’] = true;
    $fields_dup[‘isIos’] = true;
    $fields_dup[‘isAnyWeb’] = false;
    $fields_dup[‘isWP’] = false;
    $fields_dup[‘isAdm’] = false;
    $fields_dup[‘isChrome’] = false;
    /* $fields_dup[‘data’] = array(“myappurl” => $fields[‘url’]);*/
    $fields_dup[‘data’] = array(“postid” => $post->ID, “sharelink” => $fields[‘url’]);
    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    $fields_dup[‘big_picture’] = $url;
    $fields_dup[‘buttons’] = ‘[{“id”: “id1”, “text”: “Settings”, “icon”: “ic_menu_manage”}, {“id”: “id2”, “text”: “Share”, “icon”: “ic_menu_share”}]’;
    unset($fields_dup[‘url’]);

    /* Send another notification via cURL */
    $ch = curl_init();
    $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’];
    curl_setopt($ch, CURLOPT_URL, $onesignal_post_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    ‘Content-Type: application/json’,
    ‘Authorization: Basic ‘ . $onesignal_auth_key
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields_dup));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Optional: Turn off host verification if SSL errors for local testing
    // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    $response = curl_exec($ch);

    curl_close($ch);
    return $fields;
    }

  • The topic ‘Send only Application notification’ is closed to new replies.