【问题标题】:Firebase : php push notification to multiple devicsFirebase:php 推送通知到多个设备
【发布时间】:2017-12-08 12:51:09
【问题描述】:

我正在使用以下代码向一台 Android 设备发送推送通知

function send_notification($message, $token){
    define( 'API_ACCESS_KEY', 'mykey');
     $msg = array
          (
            'body'  =>$message,
            'title' => 'You have a new message ',

          );
    $fields = array
            (
                'to'        => $token, 
                '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 );
        echo $result;
        curl_close( $ch );
}

我成功地将它发送到一个设备如果我想将它发送到多个设备 ID 我必须在哪里循环以及数据格式是什么。请帮忙

【问题讨论】:

    标签: php android firebase google-cloud-messaging


    【解决方案1】:

    Docs here 显示如何为多播消息的接收者指定registration_ids。将您的 $fields['to'] 替换为 $fields['registration_ids'],这将是一个包含收件人令牌的字符串数组。

    【讨论】:

    • 应该是这样的'to' => [2,3,4,5]
    • 不@Sikander,应该是这样的:$fields = array( 'registration_ids' => [2,3,4,5], ... );
    • 怎么样?我的意思是请澄清
    【解决方案2】:

    试试这个:

    for($i=0;$i<count($TokenArray);$i++){
      send_notification($message, $TokenArray[$i]);
    }
    

    【讨论】:

    • 它不会在firebase管理面板中添加4个不同的enteries
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多