简体   繁体   中英

Firebase Web Push notification

I am trying to send web push notification using firebase in angular5 and Codeignitor. After intialize firebase, I am able to get deviceToken to whoom I want to send push notification. When I am trying to send it at backend using curl. It always show me this error

{
    "multicast_id":8778443463170192739,
    "success":0,
    "failure":1,
    "canonical_ids":0,
    "results":[
        {
            "error":"MismatchSenderId"
        }
    ]
}

In backend api request, I am using "Server key" which is "AAAA8q7RESs:APA91bFrOrzgvTb0yQTqTF6huozIvgi_5xf1lY8qv5kyoDViuBYnVFr3Qfxg3DZHN_e7-AqWBIvRlQvPJkgZtxKIpCYs_aUUiYHOKuKLLJvzxAENjKmF4mCQJb94tJV8J-vBgnv0FPPY".

But always show same error.

My backend Php code is

public function sendpushnotification() {
    $request = json_decode(file_get_contents('php://input'),true);
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
        'registration_ids'  => array($request['token']),
        'data'              => array( "message" => "Hello this is test push notification" ),
    );
    $headers = array( 
        'Authorization: key=AAAA8q7RESs:APA91bFrOrzgvTb0yQTqTF6huozIvgi_5xf1lY8qv5kyoDViuBYnVFr3Qfxg3DZHN_e7-AqWBIvRlQvPJkgZtxKIpCYs_aUUiYHOKuKLLJvzxAENjKmF4mCQJb94tJV8J-vBgnv0FPPY',
        '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 );

    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

    // Avoids problem with https certificate
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);

    // Execute post
    $result = curl_exec($ch);
    curl_close($ch);
    echo json_encode($result);    
}

Can anyone helps me. I have spend lot of time for this but not found any solution.

And I have a query. Is deviceToken/deviceID depends upon messageSenderID?

First of all try to send notification from FCM console giving deviceToken. I have used below code and it's working fine. I wrote this function in helper and used it in multiple controllers. Second thing: deviceToken/deviceID will be used for the user who will receive message.

function sendFCM($message, $id, $message_info='', $type ='') {

    $API_ACCESS_KEY = "YOUR_FCM_SERVER_KEY";

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

    $fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message,
                    'message_info' => $message_info,
            ),                
            'priority' => 'high',
            'notification' => array(
                        'title' => $message['title'],
                        'body' => $message['body'],                            
            ),
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . $API_ACCESS_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_POSTFIELDS, $fields );
    $result = curl_exec ( $ch );
    curl_close ( $ch );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM