简体   繁体   中英

how to send push notification from onesignal the device id's are stored in database - cordova app

I'm using push notification from onesignal platform in my cordova app. I have implemented it to send push notification from oneSignal dashboard but I want to send notification from my own search, I have stored device ids when user install the app but don't know how to send notifications to devices that's ids are stored in my database

Any help regarding this will be appreciated . Thanks if anyone has issue to understand my question can comment i will explain with my best capacity.

To handle this case, I also use one signal, but at the launch of my app, I send my id to my backend to be sure it will be registered to my db when I will need it. Just with a classic http post request.

There is no magic trick to achieve that, you don't have lot of choices here except save it to your db by a way or another.

You can use this function which is provided by OneSignal

function sendMessage(){
    $content = array(
        "en" => 'YOUR_CUSTOM_MESSAGE'
        );

    $fields = array(
        'app_id' => "YOUR_APP_ID",
        'include_player_ids' => array("YOUR_TARGET_ID"),
        'contents' => $content
    );

    $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);

print("\n\nJSON received:\n");
print($return);
print("\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM