【问题标题】:Firebase how to send Topic NotificationFirebase 如何发送主题通知
【发布时间】:2017-07-12 01:40:09
【问题描述】:

我正在使用以下脚本向特定用户发送通知:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'My_API_KEY' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
    'body'  => "abc",
    'title'     => "Hello from Api",
    'vibrate'   => 1,
    'sound'     => 1,
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

$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, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

脚本运行良好,但我如何向所有安装了我的应用程序的用户发送通知。我在我的应用程序(警报)中创建了一个主题,我可以通过 firebase 控制台向所有用户发送通知。谁能指导我更新上面的主题脚本。

【问题讨论】:

  • 能否提供IDE中推送通知部分的代码? !I have coded the php script part 我不知道如何向 Firebase 服务器发送请求。请分享 IDE 中处理推送通知的代码!.

标签: php android firebase firebase-cloud-messaging


【解决方案1】:

我通过替换修复了

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

$fields = array
(
    'to'  => '/topics/alerts',
    'notification'          => $msg
);

【讨论】:

  • 如果我需要发送给特定的订阅者怎么办?
  • 什么是registration_ids?或者如何找到它,我想给每个人发消息,谢谢!
  • 注册ID是每个用户唯一的令牌值
  • @DIGITALJEDI 那么如果我想向所有用户发送通知,而不是每个用户,我不确定我是否理解它
  • 那么你只需要为他们订阅一个主题,比如 'to' => '/topics/alerts' 。然后所有默认订阅的用户都会收到通知
【解决方案2】:

我多次使用 google firebase,我建议使用我的简单代码发送主题通知。

public function test()
{
    // Method - 1
    // $fcmUrl = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1';
    // $notification = [
    //     "message" => [
    //         "topic" => "foo-bar",
    //         "notification" => [
    //             "body" : "This is a Firebase Cloud Messaging Topic Message!",
    //             "title" : "FCM Message",
    //         ]
    //     ]
    // ]; 

    // Method - 2 

    $fcmUrl = 'https://fcm.googleapis.com/fcm/send';



    $notification = [
        "to" => '/topics/cbtf',
            "data" => [
                "message" : "Messaging Topic Message!",
            ]
        ]
    ];


    $headers = [
        'Authorization: key=AIza...............klQ5SSgJc',
        'Content-Type: application/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    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($notification));
    $result = curl_exec($ch);
    curl_close($ch);

    return true;
}

【讨论】:

    【解决方案3】:

    您可以在没有 curl 的情况下发送通知(这在我的服务器上不可用)。 我准备了一个可以向指定主题发送通知的函数:

    sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "new_post", "YOUR_SERVER_KEY");
    
    function sendNotification($title = "", $body = "", $customData = [], $topic = "", $serverKey = ""){
        if($serverKey != ""){
            ini_set("allow_url_fopen", "On");
            $data = 
            [
                "to" => '/topics/'.$topic,
                "notification" => [
                    "body" => $body,
                    "title" => $title,
                ],
                "data" => $customData
            ];
    
            $options = array(
                'http' => array(
                    'method'  => 'POST',
                    'content' => json_encode( $data ),
                    'header'=>  "Content-Type: application/json\r\n" .
                                "Accept: application/json\r\n" . 
                                "Authorization:key=".$serverKey
                )
            );
    
            $context  = stream_context_create( $options );
            $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
            return json_decode( $result );
        }
        return false;
    }
    

    【讨论】:

    • 如何创建主题以及如何知道这些主题下有哪些用户
    【解决方案4】:

    您可以向 firebase 中的任何主题发送通知。你可以用任何语言来做,这只是一个 http 请求,但你必须始终保持 JSON 格式,以便你可以从

    捕获来自你的 android 部分的通知
     public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    
        Timber.d("Data Payload: " + remoteMessage.getData());
    }
    

    所以你需要发送以下JSON格式

    {
     "to": "\/topics\/general",
     "data": {
     "data": {
      "title": "Database Notification",
      "message": "New Data Added"
       }
     }
    }
    

    这样就可以从remoteMessage.getData()获取这个数据集

    【讨论】:

      【解决方案5】:

      特定主题

      <?php
      
      print "testing";
      
      function sendPushnotification($data = array()) {
      
        $apiKey = '';
      
      
        $fields = array('to' => '/topics/EWAP' , 'notification' => $data);
        $headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json', 'priority' => 10);
      
        $url = 'https://fcm.googleapis.com/fcm/send';
      
        // var_dump($fields);
      
        $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));
        $result = curl_exec($ch);
        curl_close($ch);
      
        return json_encode($result, true);
      }
      
      
      $data = array(
        'title' => 'Today topic',
        'body' => 'done buddy'
      );
      
      var_dump(sendPushnotification($data));
      
      
      ?>
      

      【讨论】:

        【解决方案6】:

        使用设备令牌。

        <?php
        
        print "testing";
        
        function sendPushnotification($to = '',$data = array()) {
        
          $apiKey = '';
        
        
          $fields = array('to' => $to , 'notification' => $data);
          $headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json');
        
          $url = 'https://fcm.googleapis.com/fcm/send';
        
          // var_dump($fields);
        
          $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));
          $result = curl_exec($ch);
          curl_close($ch);
        
          return json_encode($result, true);
        }
        
        
        $to = "add device here token ";
        $data = array(
          'title' => 'Today topic',
          'body' => 'done buddy'
        );
        
        var_dump(sendPushnotification($to, $data));
        
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 2021-04-20
          • 2020-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-08
          相关资源
          最近更新 更多