Toriqul Islam Tareq
Toriqul Islam Tareq

Reputation: 472

FCM give success but Notification is not received by device in Ionic V1

I am using FIREBASE CLOUD MESSAGING service with my ionic product and phonegap-plugin-push cordova plugin to get push notification from PHP BACK END.

When I am trying to get push notification then php end is getting result with success as below.

Sample Push Data Payload

{"multicast_id":8853634389214913500,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1495614850271706%39688dd8f9fd7ecd"}]}

Technology specification :

PHP PART:

$push_title = $this->input->post('push_title');
$push_msg = $this->input->post('push_msg');
$members = $this->members_model->get_members();
$apk_tokens = array();
$ios_tokens = array();
foreach ($members as $member) {
    if ($member['apk_token'] != 0 || $member['apk_token'] != "") {
        array_push($apk_tokens, $member['apk_token']);
    }
    if ($member['ios_token'] != 0 || $member['ios_token'] != "") {
        array_push($ios_tokens, $member['ios_token']);
    }
}
//Sending the push notification using GCM.
$msg = array(
    'message' => $push_msg,
    'title' => $push_title,
    'vibrate' => 1,
    'sound' => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon',
);

$fields = array
(
    'registration_ids' => $apk_tokens,
    'data' => $msg,
    'priority' => 'high'
);

$headers = array
(
    'Authorization: MY FCM SERVER 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);
curl_close($ch);
echo $result;

Thanks in advance!

Upvotes: 4

Views: 8972

Answers (2)

Nilesh Gaikwad
Nilesh Gaikwad

Reputation: 1

As per latest update in FCM, following will be code:

$curl = curl_init(); 
$msg = [
    "message" => [
          "notification" => [ 
        "title" =>  $content->title,
                "body" => $content->notification_body,
        "image" => $content->notification_image
          ],                
                "token" => $token
            ]
    ];
$data = json_encode($msg); 

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fcm.googleapis.com/v1/projects/'. $project_id .'/messages:send',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer ' . getGoogleAccessToken($project_id),
        'Content-Type: application/json'
    ),
));
    
$response = curl_exec($curl);
    
$resp = json_decode($response);

function getGoogleAccessToken($file_name) { 
    $credentialsFilePath = base_path(). "/". $file_name .".json"; //replace this with your actual path and file name
    echo "Cred_file". $credentialsFilePath;
    $client = new \Google_Client();
    $client->setAuthConfig($credentialsFilePath);
    $client->addScope('https://www.googleapis.com/auth/firebase.messaging');
    $client->refreshTokenWithAssertion();
    $token = $client->getAccessToken();
    return $token['access_token'];
   }

Upvotes: 0

noobar
noobar

Reputation: 838

If you want to show the notification on lock screen, use notification in $fields. The object requires title and body elements.

https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol

$fields = array
(
    'registration_ids' => $apk_tokens,
    'data' => $msg,
    'priority' => 'high',
    'notification' => array(
        'title' => 'This is title',
        'body' => 'This is body'
    )
);

I did not try this code, but same issue with node.js SDK has been fixed in this way.

Upvotes: 8

Related Questions