0

I am sending a notification with the android php file, right now it is empty because I need to add channelID. How do I define Array to channelID? Empty notification going because of no channelID

<?php 
sendNotification($title,$message,"android");

function sendNotification($title="",$message="",$type="android") {
    $path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
    $server_key = "..";

    if($type=="ios"){
        // IOS KEY
        $finalKey = array('..');
    }else{
        // ANROID KEY
        $finalKey = array('..');
    }

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

    $fields = array('to'  => '/topics/...', 'notification'=>array('title'=>$title, 'message'=>$message));


    $payload = json_encode($fields);

    $curl_session = curl_init();
    curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
    curl_setopt($curl_session, CURLOPT_POST, true);
    curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($curl_session, CURLOPT_ENCODING, "");
    curl_setopt($curl_session, CURLOPT_MAXREDIRS, 10);
    curl_setopt($curl_session, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl_session, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, "POST");
    $result = curl_exec($curl_session);
    if ($result === FALSE) {
        die('Oops! FCM Send Error: ' . curl_error($curl_session));
    }
    curl_close($curl_session);
    return json_decode($result,TRUE);

}
  • Does this answer your question? [How to set notification channel when sending messages via rest api?](https://stackoverflow.com/questions/52072135/how-to-set-notification-channel-when-sending-messages-via-rest-api) – Progman Feb 01 '20 at 10:53
  • Also relevant: https://stackoverflow.com/questions/58037761/set-fcm-channel-id-in-php-for-notifications – Progman Feb 01 '20 at 10:54
  • I changed it this way, will it work? ; function sendNotification($title="",$message="",$type="android",$chid="...") $fields = array('to' => '/topics/..', 'notification'=>array('title'=>$title, 'message'=>$message, 'channelId' => $chid)); – SH Yazılım Geliştirme Feb 01 '20 at 11:33

0 Answers0