简体   繁体   中英

firebase push notification "error": "InvalidRegistration" using codeigniter

config/androidfcm.php

<?php
    defined('BASEPATH') or exit('No direct script access allowed');
    $config['key'] = 'AAAAMVoCH6k:APA91bE264kcSCOkYlJTpcLvblrlANqw9CTEFkyXwSYxoycsy7IbRINql5tJEf1VGAGu4rVAU0Y9BCIhJrirdbU4BjassnmjewYVMFmSRflJc-x1-3-RvvIOLMLchbz9UO59junbegLX';
    $config['fcm_url'] = 'https://fcm.googleapis.com/fcm/send';

controller:

<?php
    defined('BASEPATH') or exit('No direct script access allowed');
    class Users extends CI_Controller
    {
        public function sendNotification()
        {
            $token = 'Registratin_id'; // push token
            $message = "Test notification message";
            $this->load->library('fcm');
            $this->fcm->setTitle('Test FCM Notification');
            $this->fcm->setMessage($message);
            $this->fcm->setIsBackground(false);
            $payload = array('notification' => '');
            $this->fcm->setPayload($payload);
            $this->fcm->setImage('https://firebase.google.com/_static/9f55fd91be/images/firebase/lockup.png');
            $json = $this->fcm->getPush();
            $p = $this->fcm->send($token, $json);
            print_r($p);
            exit();
        }

$json:

Array ( [data] => Array ( [title] => Test FCM Notification [is_background] => [message] => Test notification message [image] => https://firebase.google.com/_static/9f55fd91be/images/firebase/lockup.png [payload] => Array ( [notification] => ) [timestamp] => 2020-05-18 15:08:40 ) )

I am using Firebase push notification in codeigniter to push real time notification. For this I am using androidfcm.php file inside my config folder and library/FCM.php and in controller I have sendnotification function for testing but it throw an error ie {"multicast_id":4322075284510288543,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]} . I don't know why? Please help me to solve this.

Thank you

You have to follow the steps to get result. STEP 2 IS IMPORTANT

step 1: create Fcm.php file in library folder and paste below code in it

       <?php
         
         class Fcm {
           function sendNotification($dataArr) {

           $fcmApiKey = "AAAARvSWcc0:APA91bHro1rOOmsESHY6T0jHkMbcXysEtqzueWDcyF22yC7JYqo-u_6xBhVyVM39pz8Gz6Z0rwI5JsoW-cuhZs-NPbnabM-qOAvl71DF1r_4F5neVLQ5ToKCetkFzfZtX59619ujvU8U" ; //App API Key

           $url = 'https://fcm.googleapis.com/fcm/send';//Google URL
           $registrationIds = $dataArr['token'];//Fcm Device ids array
           $message = $dataArr['message'];//Message which you want to send
           $title = $dataArr['title'];
           $type = $dataArr['type'];
    
          if(is_array($registrationIds)){
              
               $notificationdata = array('body' => $message,'title' => $title,'type' => $type);
               $fields = array('registration_ids' => $registrationIds ,'notification' => $notificationdata);

          }else{
            
               $fields = array('to' => $registrationIds ,'body' => $message,'title' => $title,'type' => $type);
         
          }

         $headers = array(
            'Authorization: key=' . $fcmApiKey,
            '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_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;
        }
      }
?>

step 2: get the $fcmApiKey from google firebase account where you have to register your project in it (firebase account provide the key as same as above ) and paste it

step 3: include the library in your controller or model from where you want to send the notification like this -> $this->load->library('fcm');

step 4: set the parameters as per below code and enjoy

            $notificationparam['token'] = [$token];
            $notificationparam['title'] = "hello";
            $notificationparam['message'] = "I am a developer";
            $notificationparam['type'] = 1;
            $notificationparam['flag'] = 1;

            $notification = $this->fcm->sendNotification($notificationparam);

$token = firebase_token which is get from android app developer when he / she register your email in firebase account and you must pass token in [] square braces

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