Title: Variable LaunchURL
Last modified: September 1, 2016

---

# Variable LaunchURL

 *  Resolved [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-launchurl/)
 * Hi! Plugin and OneSignal work perfectly, thank you for great work!
    But have 
   some issue. I use OneSignal with WP blog and with native android application.
   When Add/Modify post send notification to Browser and Android App. My app use
   URL Scheme ex.: myapp://. When recieve notification on mobile sended via plugin
   link open mobile browser. It’s possible send to mobile classic LaunchURL ex.:
   [http://myblog.com/](http://myblog.com/) and add field to custom LaunchURL for
   sending notify to mobile ex.: myapp://
 * [https://wordpress.org/plugins/onesignal-free-web-push-notifications/](https://wordpress.org/plugins/onesignal-free-web-push-notifications/)

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Plugin Author [OneSignal Push Notifications](https://wordpress.org/support/users/onesignal/)
 * (@onesignal)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501493)
 * Hi,
 * Sounds like you want to send a web push notification that opens in the browser,
   and also send a native Android notification that opens in the app.
 * Our WordPress plugin will automatically send a web push notification to all web
   push users. To send a native Android notification that opens in your app, you’ll
   have to use our WordPress filter hook.
 * Add this PHP code to a file that will always get called when a notification is
   being sent. Something like the index or header files of your theme might work.
 * Note: This code may not work out of the box, you may have to tweak it around 
   a bit, but the comments below should guide you on how to fix that.
 * `
    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']); unset($fields_dup['
   url']);
 * /* Send another notification via cURL */
    $ch = curl_init(); $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']; 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);
 * /* Optional: cURL settings to help log cURL output response
    curl_setopt($ch,
   CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_HTTP200ALIASES, array(400));
   curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, $out);*/
   $response = curl_exec($ch);
 * /* Optional: Log cURL output response
    fclose($out); $debug_output = ob_get_clean();
   $curl_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $curl_http_code
   = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_total_time = curl_getinfo($ch,
   CURLINFO_TOTAL_TIME); onesignal_debug('OneSignal API POST Data:', $fields); onesignal_debug('
   OneSignal API URL:', $curl_effective_url); onesignal_debug('OneSignal API Response
   Status Code:', $curl_http_code); if ($curl_http_code != 200) { onesignal_debug('
   cURL Request Time:', $curl_total_time, 'seconds'); onesignal_debug('cURL Error
   Number:', curl_errno($ch)); onesignal_debug('cURL Error Description:', curl_error(
   $ch)); onesignal_debug('cURL Response:', print_r($response, true)); onesignal_debug('
   cURL Verbose Log:', $debug_output); } */ curl_close($ch); return $fields; }
 * So what you’ve just added is a WordPress filter hook that gets applied whenever
   we send out a notification. This filter hook sends an _additional_ notification
   to Android users, and then exits, and the plugin sends its normal web push notification
   to web users.
 * In your native Android application, you need to capture this event so that your
   app can respond with whatever it needs to do. If you’re using Phonegap, Cordova,
   or Ionic, for example, here’s the event you can use to also navigate to the same
   URL:
 * [https://documentation.onesignal.com/docs/phonegap–cordova-sdk-api#notificationOpenedCallback](https://documentation.onesignal.com/docs/phonegap–cordova-sdk-api#notificationOpenedCallback)
 * The code example there has different variants. Click the “Open page in app” variant
   to see the code example.
 * Please note that the above filter hook puts the URL In the ‘additionalData’ property
   with a key of ‘myappurl’ and a value of the URL to navigate to, so you’ll have
   to look inside ‘myappurl’ to get the value you need.
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501540)
 * Thank you for fast answer. Works very good, but have some quiestion, ionic not
   support location.href need $state.go to open page, how fix it here:
 *     ```
       var notificationOpenedCallback = function(jsonData) {
         if (jsonData.additionalData) {
           if (jsonData.additionalData.yourUrlKey)
             location.href = jsonData.additionalData.yourUrlKey;
         }
       }
       ```
   
 * Maybe can help start this state onload:
 *     ```
       .state('app.post', {
           url: "/posts/:postId",
           views: {
             'menuContent': {
               templateUrl: "templates/post.html",
               controller: 'PostCtrl'
             }
           }
         })
       ```
   
 * Try like this, but not work:
 *     ```
       var notificationOpenedCallback = function(jsonData) {
         if (jsonData.additionalData) {
           if (jsonData.additionalData.myKey)
             $state.go('app.post', {'postId': + jsonData.additionalData.myKey});
         }
       }
       ```
   
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501542)
 * Resolved, just forget add $state to .run. Now all works perfectly! Maybe in next
   release yo add this functions:
    1. Send to Browser only 2. Send to Browser and
   App 3. Send to App only If select App sending add some fields: 1. Key and Value(
   value with post_type, postID) 2. Big Picture 3. Logo
 *  Plugin Author [OneSignal Push Notifications](https://wordpress.org/support/users/onesignal/)
 * (@onesignal)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501591)
 * Hey kingstakh,
 * Really glad you got it working! We’re probably going to hold off on adding that
   many options. If someone wants to add what you’re adding, we’d tell them to add
   this code instead for now.
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501740)
 * Have new issue ))) How add “actionButtons”, “bigPicture” to $fields_dup[‘data’]
   = array(“postid” => $post->ID). Try like this
 *     ```
       $fields_dup['data'] = array(
       "postid" => $post->ID,
       "bigPicture" => "http://www.mysite.net/bigpic.jpg",
       "largeIcon" => "http://www.mysite.net/img.jpg"
       );
       ```
   
 * But pictures don’t show when receive notification (((
 *  Plugin Author [OneSignal Push Notifications](https://wordpress.org/support/users/onesignal/)
 * (@onesignal)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501741)
 * Hey Kingstakh,
 * Try putting that outside the $fields_dup variable and using underscores in the
   variable name for OneSignal parameters. Only custom data should be inside [‘data’].
 * e.g.
 * $fields_dup[‘big_picture’] = ‘a url here’
 * See [https://documentation.onesignal.com/docs/notifications-create-notification](https://documentation.onesignal.com/docs/notifications-create-notification)
   for the parameters.
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-7501742)
 * Thanks for the link to the documentation, I could save a lot of time if you would
   have found it myself))) Now is all ok!
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8324344)
 * Hi! After last updates have problem. Now I always receive two notification, first
   open my app, second open link in mobile browser. Everything was working fine 
   before but now there is this problem.
    But the strangest thing is that all the
   old projects are working fine. A new have this problem. I use this code:
 *     ```
       //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";
       /* 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);
   
       /* Optional: cURL settings to help log cURL output response
       curl_setopt($ch, CURLOPT_FAILONERROR, false);
       curl_setopt($ch, CURLOPT_HTTP200ALIASES, array(400));
       curl_setopt($ch, CURLOPT_VERBOSE, true);
       curl_setopt($ch, CURLOPT_STDERR, $out);
       */
       $response = curl_exec($ch);
   
       /* Optional: Log cURL output response
       fclose($out);
       $debug_output = ob_get_clean();
       $curl_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
       $curl_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       $curl_total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
       onesignal_debug('OneSignal API POST Data:', $fields);
       onesignal_debug('OneSignal API URL:', $curl_effective_url);
       onesignal_debug('OneSignal API Response Status Code:', $curl_http_code);
       if ($curl_http_code != 200) {
       onesignal_debug('cURL Request Time:', $curl_total_time, 'seconds');
       onesignal_debug('cURL Error Number:', curl_errno($ch));
       onesignal_debug('cURL Error Description:', curl_error($ch));
       onesignal_debug('cURL Response:', print_r($response, true));
       onesignal_debug('cURL Verbose Log:', $debug_output);
       }
       */
       curl_close($ch);
       return $fields;
       }
       ```
   
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8325048)
 * My bad, all is ok!
 *  [aaronkixx](https://wordpress.org/support/users/aaronkixx/)
 * (@aaronkixx)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8621868)
 * Using this code it works a little however i have a problem…
 * i’m getting 2 notifications – one opens the app one opens a broweser how do i
   stop this?
 *  Thread Starter [kingstakh](https://wordpress.org/support/users/kingstakh/)
 * (@kingstakh)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8621886)
 * You must configure Web Platforms in you Onesignal.com App and add your Subdomain
   on Onesignal Plugin setting page.
    Don’t forget **disable** Send notifications
   additionally to iOS & Android platforms on Onesignal Plugin setting page.
    -  This reply was modified 9 years, 5 months ago by [kingstakh](https://wordpress.org/support/users/kingstakh/).
 *  [aaronkixx](https://wordpress.org/support/users/aaronkixx/)
 * (@aaronkixx)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8621912)
 * Ah brilliant!

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Variable LaunchURL’ is closed to new replies.

 * ![](https://ps.w.org/onesignal-free-web-push-notifications/assets/icon.svg?rev
   =3003897)
 * [OneSignal - Web Push Notifications](https://wordpress.org/plugins/onesignal-free-web-push-notifications/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/onesignal-free-web-push-notifications/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/onesignal-free-web-push-notifications/)
 * [Active Topics](https://wordpress.org/support/plugin/onesignal-free-web-push-notifications/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/onesignal-free-web-push-notifications/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/onesignal-free-web-push-notifications/reviews/)

 * 12 replies
 * 3 participants
 * Last reply from: [aaronkixx](https://wordpress.org/support/users/aaronkixx/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/variable-launchurl/#post-8621912)
 * Status: resolved