【问题标题】:Set ID in FCM notification在 FCM 通知中设置 ID
【发布时间】:2022-01-19 11:09:39
【问题描述】:

我正在开发一种向 Android 手机发送 FCM 通知的工具。我正在使用 Firebase 云消息传递来执行此操作。我通过 PHP 和 CURL 发送通知。我希望所有通知都带有相同的标识符,因为每次发送它们时,都会生成一个新的,我希望它被替换。


/*PHP CURL*/

$to="TOKEN USER";
$apiKey="KEY APPLICATION";

$notification= array(
            'title'=>'TITLE',
            'body' => 'BODY',
         /* HERE SHOULD I ADD THE ID? */
);

$ch = curl_init();

    $url="https://fcm.googleapis.com/fcm/send";
    $fields=json_encode(array('to'=>$to,"notification"=>$notification));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $headers = array();
    $headers[] = 'Authorization: key ='.$apiKey;
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

【问题讨论】:

    标签: php curl firebase-cloud-messaging android-notifications


    【解决方案1】:

    好的,我找到了解决方案,使用 collapse_key

    $fields=json_encode(array('to'=>$to,"notification"=>$notification,"collapse_key"=>"new_messages"));
    
      $notification= array(
                'title'=>'Title',
                'body' => 'Body', 
                'tag' => 'new_messages'   
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2018-02-15
      • 2019-07-07
      • 2019-04-13
      • 2021-07-06
      • 1970-01-01
      相关资源
      最近更新 更多