第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中為通知設(shè)置 FCM 通道 ID

在 PHP 中為通知設(shè)置 FCM 通道 ID

PHP
SMILET 2022-01-23 10:45:41
我正在嘗試使用 PHP 向 Android 設(shè)備發(fā)送 FCM 通知。我的代碼適用于 Android O 之前的設(shè)備。在 Android O 中,我們還需要在請求中設(shè)置通道 ID 以接收通知,我不知道該怎么做。我已經(jīng)在應(yīng)用程序中完成了必要的設(shè)置并使用 Firebase 控制臺我可以收到通知,但是當(dāng)我嘗試通過 PHP 腳本發(fā)送它時它失敗了。我還查看了下面的鏈接,但它對我不起作用。我的PHP代碼:    $notification = new Notification();    $notification->setTitle("startSession");    $notification->setBody($_POST['child_id']);    $notification->setNotificationChannel('my_channel_id');    $requestData = $notification->getNotification();    $firebase_token = $_POST['token'];    $firebase_api = 'my_value';    $fields = array(                     'to' => $firebase_token,                     'data' => $requestData,                   );    $url = 'https://fcm.googleapis.com/fcm/send';    $headers = array(        'Authorization: key=' . $firebase_api,        'Content-Type: application/json'    );    $ch = curl_init();    // Set the url, number of POST vars, POST data    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_POST, true);    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));    // Execute post    $result = curl_exec($ch);    if($result === FALSE){        die('Curl failed: ' . curl_error($ch));        $return_obj = array('responseCode'=>'0' , 'responseMsg'=> 'Error inserting, please try          again.', 'errorCode'=>'1');    }else{        $return_obj = array('responseCode'=>'1' , 'responseMsg'=> 'Successfully inserted.',         'errorCode'=>'0');    }    // Close connection    echo json_encode($return_obj);    curl_close($ch);我確信語法是正確的,因為通知仍然適用于 Android < 26 的設(shè)備。任何幫助將不勝感激。謝謝!
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

您可以在服務(wù)器端 PHP 中設(shè)置您的頻道 ID


PHP服務(wù)器端


<?php

  function Notify($title,$body,$target,$chid)

   {


    define( 'API_ACCESS_KEY', 'enter here your API Key' );


  $fcmMsg = array(

    'title' => $title,

    'body' => $body,

    'channelId' => $chid,


  );

  $fcmFields = array(

    'to' => $target, //tokens sending for notification

    'notification' => $fcmMsg,


  );


  $headers = array(

    'Authorization: key=' . API_ACCESS_KEY,

    'Content-Type: application/json'

  );


$ch = curl_init();

curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );

curl_setopt( $ch,CURLOPT_POST, true );

curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );

curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );

curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fcmFields ) );

$result = curl_exec($ch );

curl_close( $ch );

echo $result . "\n\n";


}


?>

我Notify用4個參數(shù)定義函數(shù)title,body,target,chid來使用它只是調(diào)用函數(shù)并定義它的參數(shù),


您必須在客戶端(Android 部分)中添加您的 channelId,如果沒有 channelId,您將無法在 Android 8.0 及更高版本中收到通知


查看完整回答
反對 回復(fù) 2022-01-23
  • 1 回答
  • 0 關(guān)注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號