简体   繁体   English

通过 cURL/PHP 发送时,设备上未收到 Firebase 通知

[英]Firebase Notification not received on device when sent via cURL/PHP

I have an iOS app which is live on the app store.我有一个在应用商店上线的 iOS 应用。 I am able to send push notifications to iOS devices which have the app installed, but only when I send them from the Firebase console.我可以向安装了该应用程序的 iOS 设备发送推送通知,但仅当我从 Firebase 控制台发送它们时。

When I try to send push notifications via a cURL request, the response from the server indicates that I was successful but the message isn't received on the device.当我尝试通过 cURL 请求发送推送通知时,来自服务器的响应表明我已成功,但设备上未收到消息。 I have tried this with both multicast and single recipient payloads.我已经尝试使用多播和单接收者有效负载。

I must be missing something more fundamental, but I can't see it.我一定遗漏了一些更基本的东西,但我看不到它。

Here is my PHP code:这是我的PHP代码:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AI*****4LPGkx8xtDG2tBl*****7KWJfmp1szcA' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title'
);
$fields = array
(
    'to'            => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_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;

Here is the response I get when running this code:这是我在运行此代码时得到的响应:

{"multicast_id":5814921248239922706,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1476193002715692%a4ddee3cf9fd7ecd"}]} {"multicast_id":5814921248239922706,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1476193002715692%a4ddee7ecd"}f9

There were two issues:有两个问题:

  1. I needed to include a notification section in the payload instead of data我需要在有效负载中包含一个notification部分而不是data
  2. Somehow the payload wasn't being formatted properly by PHP.不知何故,PHP 没有正确格式化有效负载。

In the end I used the PHP function shell_exec() to do a cURL request over SSH instead.最后,我使用 PHP 函数shell_exec()通过 SSH 执行 cURL 请求。 This isn't ideal but it got the job done.这并不理想,但它完成了工作。

Example code:示例代码:

shell_exec('curl -X POST --header "Authorization: key=<key here>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"'.$to.'\",\"priority\":\"high\",\"notification\":{\"body\": \"'.stripslashes($message).'\"}}"');

From the Firebase documentation you have the choice of using either data or notification in the message payload.从 Firebase 文档中,您可以选择在消息负载中使用数据通知 But when you use data , you have the responsibility of handling the receipt of the notification yourself.但是当您使用数据时,您有责任自己处理接收通知。 In other words it will not be sent straight away to your app client.换句话说,它不会立即发送到您的应用程序客户端。 You handle it in the didReceiveRemoteNotification: for ios and onMessageReceived() in case of Android .您在didReceiveRemoteNotification:处理它,对于iosonMessageReceived()Android 的情况下。

If however you use notification in the payload, firebase will send the message straight away to your client App.但是,如果您在有效负载中使用通知,firebase 会立即将消息发送到您的客户端应用程序。

That is why you will not receive the message(in case you used data ) even if your curl request tells you it has succeeded in making the request.这就是为什么即使您的 curl 请求告诉您它已成功发出请求,您也不会收到消息(如果您使用了data )。

Try to add { priority : high }尝试添加{优先级:高}

$fields = array
(
    'to'            => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
    'data'          => $msg,
    'priority'      =>'high'
);

this is what i used :这是我使用的:

<?php

//Server keys is now the requirement not the legacy key since its not available //服务器密钥现在是要求而不是遗留密钥,因为它不可用

define( 'API_ACCESS_KEY', 'AAAA9....sQ:APA..HKb_Qfz.....q_k4NJonXd...9r-Fof....dJ_ylYLbR....NFz-uQRve.....0GoNISAA......SKx8G_4M...n' );
$messsage = array
(
    'body'   => 'msg',
    'title'     => 'title',
    'key1'  => 'val1'
);
$fields = array
(
    'to'            => "MdS.....Jg:APA91bEhZ........wkZ7rVmt6UmTc4T189M6ie39COvVmqsIA2DEijt_o9lHUGZbSFez......rPqNo4W_Ru............BEGEo6cuWOKu2vasQju",
    'notification'          => $message

);

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

you can add keys and values like example and handle it in your program您可以添加键和值,例如 example 并在您的程序中处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM