FCM - 检查是否已发送通知

时间:2017-11-08 10:39:09

标签: php firebase notifications firebase-cloud-messaging

我正在处理向Firebase云消息传递服务发送通知的PHP服务器。我正在使用https://github.com/brozot/Laravel-FCM

我使用示例代码从PHP发送通知:

$notificationBuilder = new \LaravelFCM\Message\PayloadNotificationBuilder('my title');
$notificationBuilder->setBody('Hello world')
                    ->setSound('default');
$notification = $notificationBuilder->build();

$topic = new \LaravelFCM\Message\Topics();
$topic->topic('news');
$topicResponse = \FCM::sendToTopic($topic, null, $notification, null);
$topicResponse->isSuccess(); //this returns true
$topicResponse->shouldRetry(); //this returns false
$topicResponse->error(); //this returns null

我尝试检查通知是否以两种方式正确发送: - 使用javascript客户端,但我收到错误 - 需要https,我正在使用localhost - 在“成长”下查看https://console.firebase.google.com - >通知,但在我添加iOS / Android应用程序之前我看不到任何内容,我现在还做不到(他们还在制作中)

我也尝试用CURL做到这一点,我找到了这个:How can I confirm that firebase notification was actually sent (fcm)?。但是我收到身份验证错误:“请求缺少必需的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。”我在标题中使用“Authorization:key = AAA ....”。我在laravel-fcm库中用作FCM_SERVER_KEY的相同密钥是正确的,因为如果我更改它,我会收到错误。

我错过了什么或者我真的无法在任何地方看到发送通知的列表吗?

编辑:使用curl时我使用了错误的URL(文档中的URL,里面有项目ID)。现在我没有得到授权错误。但问题仍然存在。

1 个答案:

答案 0 :(得分:0)

检查两次有效负载格式是否合适。使用ARC [高级REST客户端]检查通知是否已手动发送。

这是我的代码使用curl希望它有帮助

<?php

$responsearray = sendFCMnotification($ANDROIDPUSHNOTIFYAUTHKEYFCM,$payloadFormate,$pushMessage);
if($responsearray['response']['info'] == 200){
    if ($resposeMessage && $responsearray['response']['failure'] == 1 ) {
        // here failue response 
    }else if($response['message_id'] && $responsearray['response']['success'] == 1 ){
        // here success response
    }
}

function sendFCMnotification($authCode,$message, $id) {
    $headers = array (
            'Authorization: key='.$authCode,// here Assigning Authentication Key
            'Content-Type: application/json'
    );
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array (
            'registration_ids' => array ($id), // here Mobile Registration ID
            'collapse_key'=>'Test Message',
            'data' => array (
                    "payload" => $message
            )
    );  
    $ch = curl_init ();
    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_SSL_VERIFYPEER, false);
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $res_json   = curl_exec ( $ch );
    $result     = get_object_vars(json_decode($res_json));
    $code       = array('info' => curl_getinfo($ch, CURLINFO_HTTP_CODE));
    $merge1     = array_merge($code,$fields);
    $merge      = array_merge($result,$merge1);
    $reposeDet  = array('response' => $merge);
    curl_close ( $ch );
    return $reposeDet;
}
?>

有效载荷格式:

{
  "registration_ids": [
    "eGHYv_OI5ak:APA91bGM99ttwgJVZNOTC3rrkZYchYHyDDyLFIGOgDZiDXXZcYOd12nPH3cbCsokKCRozswE-nKx8tn-KY2Sw-YZ7GL117elxTOczSjaGiuk8ZI3CnsYmmn3BQ-PEHQl58PWjdUl4x56"
  ],
  "collapse_key": "",
  "data": {
    "payload": {
      "EncryptId": "SjaGiuk8ZI3CnsYmmn3BQ-PEHQl58PWjdUl4x56",
      "messagetype": "1",
      "imageurl": "",
      "url": "https://www.stackoverflow.com/mymessages/?part=RIALL",
      "message": "Someone Send Message",
      "landing": ""
    }
  }
}