使用Flutter + OneSignal为PlayerID发送推送通知

问题描述 投票:0回答:2

我正在创建一个推式日历式推送系统。我需要在为用户创建计划时系统只向他发送通知,我创建了系统来管理这个PHP,有谁知道如何帮助我?

php flutter cakephp-3.0 onesignal
2个回答
0
投票

我远不是移动应用专家,所以有人应该纠正/确认这一点。

要在您的应用程序中完成推送通知,您可以使用“实时”连接(例如websocket),或者您可以使用轮询。

我对websockets知之甚少,我不认为CakePHP可以做到这一点(不确定)。编辑:绝对不可能开箱即用,但存在插件。

当您使用轮询时,每隔一段时间(每小时一次,每分钟一次,根据需要)重复一次GET请求,并检查是否有新信息。 例如,您的CakePHP页面可能是一个采用lastUpdated参数的操作,该参数从该时间戳开始返回新信息。然后应用程序每隔x分钟请求此页面,每次设置lastUpdated参数。当有新信息时,响应将不为空,应用程序可以处理它。

这意味着应用程序需要始终在后台运行,并且请求数量可以变得相当大(取决于轮询间隔)。


0
投票

如果您正在使用OneSignal。您可以使用Playerid发送到该个人设备,但是您必须存储playerid服务器端,以便您知道要发送到哪个设备。我个人在init状态下执行此操作并对我的api执行http.post以将playerid保存到我的数据库中以供该特定用户使用。

您当然可以通过使用OneSignal的标签来实现相同的目标(如果同一个人在一个设备中有多个帐户,则非常有用)。

要发送通知,请在php中使用curl。

<?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");
?>

在Flutter中,获取包,导入它并:

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);
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.