How to make onesignal plugin send notification only to mobile app?

586 Views Asked by At

I have an ionic android app connected to OneSignal, and WordPress with which I make posts through my WordPress site. I installed the OneSignal plugin and put the customised code provided by Onesignal to send notification only to the mobile app and not to the browser. However, when I click the notification it takes me to the web instead of the app.

The below code is for sending notification only to mobile app. When I use this code I get notification but it takes me to web. The code is pasted below from their documentation.

<?php
function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
    $fields['isAndroid'] = true;
    $fields['isIos'] = true;
    $fields['isAnyWeb'] = false;
    $fields['isChrome'] = false;
    $fields['data'] = array(
        "myappurl" => $fields['url']
    );
    /* Unset the URL to prevent opening the browser when the notification is clicked */
    unset($fields['url']);
    return $fields;
}
1

There are 1 best solutions below

0
On

try this.

<?php
add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);

function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{

    $fields = $fields;
    $fields['isAndroid'] = true;
    $fields['isIos'] = true;
    $fields['isAnyWeb'] = false;
    $fields['isWP'] = false;
    $fields['isAdm'] = false;
    $fields['isChrome'] = false;
    $fields['data'] = array(
        "myappurl" => $fields['url']
    );
        /* Unset the URL to prevent opening the browser when the notification is clicked */
    unset($fields['url']);
    return $fields;
}