【问题标题】:push notification to specific user in PHP在 PHP 中向特定用户推送通知
【发布时间】:2021-12-25 19:07:48
【问题描述】:

我想使用 PHP 向特定用户发送推送通知。 能够通过 OneSignal 向所有用户发送推送通知,但无法将其发送给特定用户。 已经使用网页视图创建了 android 应用 根据文档,它需要设备 ID,但不知道通过 php 获取。

1> 是否需要为我的应用添加另一个库来推送通知。 2> 如何通过 php 获取推送通知所需的设备 ID 3> 如何为特定用户做推送通知

框架:Laravel 应用:安卓

    <?PHP
function sendMessage() {
    $content      = array(
        "en" => 'English Message'
    );
    $hashes_array = array();
    array_push($hashes_array, array(
        "id" => "like-button",
        "text" => "Like",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://yoursite.com"
    ));
    array_push($hashes_array, array(
        "id" => "like-button-2",
        "text" => "Like2",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://yoursite.com"
    ));
    $fields = array(
        'app_id' => "my_app_id",
        'included_segments' => array(
            'Subscribed Users'
        ),
        'data' => array(
            "foo" => "bar"
        ),
        'contents' => $content,
        'web_buttons' => $hashes_array
    );
    
    $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 Auth Key'
    ));
    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);

$data = json_decode($response, true);
print_r($data);
$id = $data['id'];
print_r($id);

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

Send push notification to specific user using Parse

Send Notification to specific user using onesignal

Onesignal 文档 https://documentation.onesignal.com/reference/create-notification

【问题讨论】:

    标签: android push-notification laravel-7 onesignal


    【解决方案1】:

    根据他们的 API documentation ...可以发送到不同的段:EMAILUSER TAGTEST USERS。他们对filters 的示例代码提示(不存储令牌的唯一选择):

    $fields = [
        ...
        'filters' => [
            ["field" => "external_user_id" "relation" => "=", "value" => $user->id]
        ]
    ];
    

    他们的FAQ 也包含您的问题:

    如何向单个用户发送通知?

    在 PHP 中这应该没有什么不同,虽然它清楚地暗示了“你的数据库”(Eloquent Model),它应该能够将 int $user_id 转换为 stringstring[] $registration_id aka设备注册令牌。这意味着您的 MessagingService 实现需要告诉 Laravel API 他们当前的令牌 - 否则在发送时这些将是未知的。

    我宁愿给一个 PHP 问题的 Java 答案:

    使用方法 .setExternalUserId() 设置 Laravel int $user_id,一旦它被移动客户端知道。当使用external_user_id作为过滤条件时,还应设置identity verification


    通常可以选择存储令牌,然后再次查找它们
    ...或使用.setExternalUserId(),然后可以将其用作过滤条件。

    移动客户端要么必须传递它的令牌 - 要么是 int $user_id / string $email

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多