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';
控制器:
<?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 ) )
我正在使用代码点火器中的 Firebase 推送通知来推送实时通知。为此,我在我的config
文件夹和library/FCM.php
中使用androidfcm.php
文件,在控制器中,我有sendnotification
测试功能,但它抛出了一个错误,即{"multicast_id":4322075284510288543,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
.我不知道为什么?请帮我解决这个问题。
谢谢
您必须按照步骤获得结果。第 2 步很重要
步骤1:在库文件夹中创建Fcm.php文件并将以下代码粘贴到其中
<?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;
}
}
?>
步骤2:从Google Firebase帐户获取$fcmApiKey,您必须在其中注册项目(Firebase帐户提供与上述相同的密钥(并将其粘贴
第 3 步:将库包含在要发送通知的控制器或模型中,如下所示 -> $this->加载>库('FCM'(;
步骤4:按照以下代码设置参数并享受
$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 当 Android 应用程序开发人员在 Firebase 帐户中注册您的电子邮件时,他/她会从他/她那里获得,您必须在 [] 方括号中传递令牌