我遇到一个奇怪的问题,我不知道该如何解决。我正在尝试使用Firebase云消息传递将推送通知发送到设备。即使使用Google API,我也无法发送它。 Firebase始终会回答“ 404 / v1 / my-app / messages:在此服务器上未找到发送”,这就是我们所知道的。下面是我的代码
class PushNotification {
static private $firebase_message = NULL;
public static function get_instance()
{
putenv('GOOGLE_APPLICATION_CREDENTIALS='.APPPATH.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'credentials.json');
if ( ! empty($firebase_message))
return self::$firebase_message;
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(Google_Service_FirebaseCloudMessaging::CLOUD_PLATFORM);
//
// $client->authorize();
//$client->setDeveloperKey(Kohana::$config->load('firebase.key'));
self::$firebase_message = new Google_Service_FirebaseCloudMessaging($client);
return self::$firebase_message;
}
private static function _init_request($params)
{
$request = new Google_Service_FirebaseCloudMessaging_SendMessageRequest();
$message = new Google_Service_FirebaseCloudMessaging_Message();
$notification = new Google_Service_FirebaseCloudMessaging_Notification();
if (empty($params['body']) || empty($params['title']))
throw new Kohana_Exception();
$notification->setBody($params['body']);
$notification->setTitle($params['title']);
if ( ! empty($params['topic']))
{
$message->setTopic($params['topic']);
}
if ( ! empty($params['token']))
{
$message->setToken($params['token']);
}
$message->setData($params['data']);
$message->setNotification($notification);
$request->setMessage($message);
return $request;
}
public static function send_message_to_topic($topic, $body, $title, $data = [])
{
try
{
$project_id = Kohana::$config->load('firebase.project_id');
/** @var Google_Service_FirebaseCloudMessaging $fcm */
$fcm = self::get_instance();
$request = self::_init_request([
'topic' => $topic,
'body' => $body,
'title' => $title,
'data' => $data
]);
$fcm->projects_messages->send($project_id, $request);
}
catch (Exception $exception)
{
echo $exception->getMessage();
return FALSE;
}
return TRUE;
}
public static function send_message_to_device($device_token, $body, $title, $data = [])
{
try
{
$project_id = Kohana::$config->load('firebase.project_id');
/** @var Google_Service_FirebaseCloudMessaging $fcm */
$fcm = self::get_instance();
$request = self::_init_request([
'token' => $device_token,
'body' => $body,
'title' => $title,
'data' => $data
]);
$fcm->projects_messages->send($project_id, $request);
}
catch (Exception $exception)
{
echo $exception->getMessage();
return FALSE;
}
return TRUE;
}
public static function set_conditional_message($condition, $body, $title, $data = [])
{
try
{
$project_id = Kohana::$config->load('firebase.project_id');
/** @var Google_Service_FirebaseCloudMessaging $fcm */
$fcm = self::get_instance();
$request = self::_init_request([
'condition' => $condition,
'body' => $body,
'title' => $title,
'data' => $data
]);
$fcm->projects_messages->send($project_id, $request);
}
catch (Exception $exception)
{
return FALSE;
}
return TRUE;
}
}
这就是我所说的:
$token = 'TOKEN_ID';
PushNotification::send_message_to_device($token, 'Test body', 'Test title');