为什么我的 FireBase 推送通知对 Xamarin 应用为空

Why my FireBase Push Notification comes empty to Xamarin App

提问人:Giuliano Caruso 提问时间:11/17/2023 更新时间:11/17/2023 访问量:32

问:

我在 PHP API 中使用 curl 发送通知,但 Visual Studio 中的 Xamarin 有这个问题:

System.Collections.Generic.KeyNotFoundException:“字典中不存在给定的键”

我在后端尝试过这个功能:

public function enviarDataPush($message,$title){
     $path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
     $server_key = "AAAA5EVszNI:APA91bHlVTas0jh4aB0iBiszwLuWgFwxiJZzAHhU6e-kLlD_JwLlWMdf8dzEb1mji1kWYGUCh9g2E1vvqGydsDf5ySxNeHb2esoA_cd_KXPtsSXyTcgtaupkyEXfLd3ivizwETmzITDZ";
     
     $deviceToken = "eK5iGGX_QT2XKcTzn4x2LY:APA91bGyG1QpKgEo-VMMRKhBSAn9P7p0-_tKDiXvCZeT53yqAvmy6WwMTMS6TsLyIIM843lxbz0_7LtRV4SFVywBvqgOa6nBZC70MlTGBYQbuUl7Zi7SUDL9vlwgCabRlXrOeqnF8Tek";
     $headers = array(
         'Authorization:key=' .$server_key,
         'Content-Type:application/json'
     );
     $fields = array('to'=>$deviceToken,
     'notification'=>array('title'=>$title,'body'=>$message));

     $payload = json_encode($fields);

    // echo $payload;

     $curl_session = curl_init();
     curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
     curl_setopt($curl_session, CURLOPT_POST, true);
     curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
     curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
     $result = curl_exec($curl_session);
     return $result;
 }
$message = "Hola desde PHP 2";
$title = "Notificaciones descript";
//este es el registro que te explico en word
$json='{"userID":"49","token":"","rol":"200"}';
//url de la base de datos
$url='https://sistemaonline-79c63-default-rtdb.firebaseio.com/SocioClave.json';
$envio = new aFireBase($json,$url);
//$dispositivos=$envio->obtenerTokens();
//echo $dispositivos;

$resultadoPush=$envio->enviarDataPush($message,$title);

但是我有:

  public override void OnMessageReceived(RemoteMessage message)
        {
            IDictionary<string, string> MensajeData = message.Data;
          
            string Titulo = MensajeData["title"];
            string SubTitulo = MensajeData["body"];

            androidNotification.CrearNotificacionLocal(Titulo, SubTitulo);

        }

如您所见,我在字典中转换数据,但出现此错误:

System.Collections.Generic.KeyNotFoundException:“字典中不存在给定的键。

它指的是标题和正文吗?它们在 php 和 Xamnarin 中用相同的词调用

PHP Xamarin

评论

0赞 Jason 11/17/2023
你有没有真正检查过的内容?message.Data

答: 暂无答案