0

I'm creating a push, calendar-style push system. I need when I create a schedule for the user the system send a notification only to him, I created the system to manage this PHP, does anyone know how to help me?

4

2 回答 2

0

I'm far from an expert on mobile apps, so somebody should correct / confirm this.

To accomplish push notification in your app you could use a 'live' connection (websocket for instance) or you could use polling.

I don't know much about websockets and I don't think that's possible with CakePHP (not sure). EDIT: Definitely not possible out-of-the-box, but plugins exist.

When you are using polling you repeat a GET request every so often (once per hour, once per minute, depending on needs) and check if there is new info.
For instance, your CakePHP page could be an action taking a lastUpdated argument, which returns new information since that timestamp. The app then requests this page every x minutes, each time setting the lastUpdated parameter. When there is new info, the response will be not empty and the app can process it.

This does mean the app needs to always run in the background and the number of requests can become sizeable (depending on the polling interval).

于 2019-03-19T20:00:30.620 回答
0

If you're using OneSignal. You can send to that individual device with Playerid, you do however have to store the playerid serverside so that you know which device to send to. I personally do that in init state and do a http.post to my api to save the playerid into my database for that particular user.

You can of course achieve the same by using OneSignal's tags (useful if same person has multiple accounts in one device).

To send notification, use curl in php.

<?php
function sendMessage(){
    $content = array(
        "en" => 'English Message'
        );

    $fields = array(
        'app_id' => "your-app-id",
        'include_player_ids' => array("playerid-you-want-to-send-to"),
        'data' => array("foo" => "bar"),
        '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'));
    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");
?>

In Flutter, get the package, import it and:

void oneSignal() {
OneSignal.shared.init("app-id");

OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) 

   {
     //do what you need to do with upcoming notification, get title for example
     print(notification.payload.title);
   }
}
于 2019-04-18T22:17:55.097 回答