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.: https://myblog.com/ and add field to custom LaunchURL for sending notify to mobile ex.: myapp://https://www.ads-software.com/plugins/onesignal-free-web-push-notifications/
-
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
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.
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}); } }
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. LogoHey 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.
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" => "https://www.mysite.net/bigpic.jpg", "largeIcon" => "https://www.mysite.net/img.jpg" );
But pictures don’t show when receive notification (((
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 for the parameters.
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!
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; }
My bad, all is ok!
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?
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 7 years, 10 months ago by kingstakh.
Ah brilliant!
- The topic ‘Variable LaunchURL’ is closed to new replies.