Jinhyeon Park
Jinhyeon Park

Reputation: 53

Can't send FCM PUSH using App server

First, I'm lacking in English. please, understand me..

I want to use FCM to PUSH notifications for users. So, I made an Android application which can receive Push notifications from the Firebase console, but I want to use FCM by using App Server not Fire console.

So, I made a PHP server and connected it to MYSQL. MYSQL has tokens which were created for each user. And I push FCM messages with them. I can see that it works properly, but my phones can't get PUSH notifications.

What should I do? Please, help me!

  <?php

// request FCM
function send_notification ($tokens, $message)
{

    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
            'registration_ids' => $tokens,
            'data' => $message
    );

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

    $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_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);

    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    curl_close($ch);

    return $result;
}

// Connect database and get tokens and then call send_notification method

include_once 'config.php';
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$sql = "Select Token From users";

$result = mysqli_query($conn,$sql);
$tokens = array();

if(mysqli_num_rows($result) > 0 ){

    while ($row = mysqli_fetch_assoc($result)) {
        $tokens[] = $row["Token"];
    }
}

mysqli_close($conn);

$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>

Upvotes: 1

Views: 606

Answers (1)

Jinhyeon Park
Jinhyeon Park

Reputation: 53

I forgot that I have to write part in the manifest !.... sorry my It was my mistake. So, that server's code operates well !

And Here is my sample code about manifest

enter code here


<service android:name=".utils.service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service android:name=".utils.service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

Upvotes: 0

Related Questions