简体   繁体   中英

Invalid registration while sending push notification in php

I am following this Firebase Cloud Messaging (FCM) Server Side Implementation. . but got an error of invalidregistrationid .

Did googling but found nothing. I also found some related problem which already answered on StackOverflow. but got no solutions. the following link I visited for the solution.

  1. a link .
  2. B link .
class abc{
  function send_android_notification($registration_ids="2fa86bbc8bc4aee204255a0d84461c000a26442312d0a9b1df1664d461fd3b7d") {
      $message = array('title' => 'CodeCastra', 'body' => "hi huye" ,'sound'=>'Default','image'=>'Notification Image' );
      define("GOOGLE_API_KEY", "AIzaSyDDvpPcWfOTVkkvA-yQg4yTYzIuh0MNIRw"); //legacy server key
      $fields = array(
          'registration_ids' => (array)$registration_ids,
          'notification' => $message, //note: body & title fileds must be specified for the message or your only get just the vibration but the notification
      );
      $headers = array(
          'Authorization: key=' . GOOGLE_API_KEY, //  FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
          'Content-Type: application/json'
      );
      //Open connection
      $ch = curl_init();
      //Set the url, number of POST vars, POST data
      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_SSL_VERIFYHOST, 0);
      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));
      //echo json_encode($fields);
      //print_r($headers);
      //exit;
      //Execute post
      $result = curl_exec($ch);
      if ($result === false) {
          die('Curl failed:' . curl_errno($ch));
      }
      // Close connection
      curl_close($ch);
      return $result;
  }

}

print_r((new abc)->send_android_notification());
die;
{"multicast_id":4707836736661373968,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

Any help will be appreciated. thanks

I am implementing your code.

Use firebase registration token it generated in device (ios/android) instead of the Device token . 'registration_ids' => (array)$token,

Hope it's helpful

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