How to send push notifications to multiple devices using php script using FCM?

前端 未结 5 1399
-上瘾入骨i
-上瘾入骨i 2021-01-12 05:55

I\'m new to push notifications using FCM from php to Android devices. From android side I have generated FCM reg_id & send it over php script & store into mysql data

5条回答
  •  隐瞒了意图╮
    2021-01-12 06:27

    Below is the simple script to send notification. Try this.

     '2',
       'redirection_type' => 'post_page' //'post_page','category_page','blog_page'
    );
    push_notification_android($tokens,$title,$msg,$customParam);
    function push_notification_android($tokens,$title,$msg,$customParam) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $api_key = 'fcm_server_api_key';
    $messageArray = array();
    $messageArray["notification"] = array (
        'title' => $title,
        'message' => $msg,
        'customParam' => $customParam,
    );
    $fields = array(
        'registration_ids' => $tokens,
        'data' => $messageArray,
    );
    $headers = array(
        'Authorization: key=' . $api_key, //GOOGLE_API_KEY
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        echo 'Android: Curl failed: ' . curl_error($ch);
    }
    // Close connection
    curl_close($ch);
    return $result;
    }
    ?>
    

提交回复
热议问题