【问题标题】:Push messages via curl wrong datas通过 curl 错误数据推送消息
【发布时间】:2017-07-24 18:36:03
【问题描述】:

我创建了一个推送消息程序,它使用 firebase.google.com。 如果通过 javascript 发送推送消息,则消息是完美的,但如果我使用 Php curl 发送推送消息,则消息与我在 php 文件中编写的消息不同。 如果我通过 PHP curl 发送,消息将是我在 service-worker.js 中写的。 为什么会这样?

php代码

function send_push_message($subscription){
if (empty($subscription)) return FALSE;
$ch = curl_init();
  switch ($subscription["browser"]){
      case "firefox":
          curl_setopt($ch, CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"] );
          curl_setopt($ch, CURLOPT_PUT, TRUE);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array( "TTL: 86400" ) );
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      break;

      case "chrome":

      /**
        * https://console.firebase.google.com
        * http://stackoverflow.com/questions/37427709/firebase-messaging-where-to-get-server-key
        */
          // API access key from Google API's Console
          define( 'API_ACCESS_KEY', '<API_SZERVER_KULCSOM>' );

            $registrationIds = array($_GET["id"]);
          // prep the bundle
          $msg = array
          (
              'message'   => 'My text',
              'title'     => 'This is a title. title',
              'subtitle'   => 'This is a subtitle. subtitle',
              'tickerText'   => 'Ticker text here...Ticker text here...Ticker text here',
              'vibrate'   => 1,
              'sound'     => 1
          );
          $fields = array
          (
              'registration_ids'   => $registrationIds,
              'data'       => $msg
          );

          $headers = array
          (
              'Authorization: key=' . API_ACCESS_KEY,
              'Content-Type: application/json'
          );

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, "https://gcm-http.googleapis.com/gcm/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, false );
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
          $response = curl_exec($ch);
          echo $response;
          if($response === false){
              echo 'Hiba: '.curl_error($ch);
          }else{
              echo 'Siker';
          }
          curl_close($ch);
      break;  
  }
}
$tomb["browser"] = "chrome";
$tomb["id"] = $_GET["id"];
if(isset($_GET["id"])){
  send_push_message($tomb);
}

service-worker.js

self.addEventListener('push', function(event) {
var title = 'xxxxxx';
var body = 'Szerbusz YYY';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
  self.registration.showNotification(title, {
    body: body,
    icon: icon,
    tag: tag
  })
);
});

谢谢

【问题讨论】:

  • “消息将是我在 service-worker.js 中写的”,因为您的值是静态的。 JS 不会自动为您替换这些值。您必须编写代码以从事件数据负载中设置正文、图标等

标签: javascript php curl


【解决方案1】:

谢谢你, 当访客注册时,我不通过 javascript 发送消息。 例如,我在 5 天后通过 Php Curl 发送消息。 发送消息将是我写的 javascript。当我发送消息时,5天后我只使用php curl!

如果不使用,为什么 PHP curl 中的 $msg 值?

谢谢

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2014-07-23
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多