Laravel生产API中的FCM Payload null

时间:2017-11-22 11:00:15

标签: php android laravel push-notification firebase-cloud-messaging

我有一个Laravel API,可以通过FCM使用laravel-push-notifications框架发送推送通知。在我的localhost中,我收到带有数据有效负载的消息,但在生产模式下,相同的代码在接收消息时返回有效负载null。

这是Laravel发送推送通知的方法:

public static function sendPushNotification($deviceToken, $actionId, $text) {
    $deviceToken = (string) $deviceToken;

    try {
        $message = \PushNotification::Message($text, array(
                    'badge' => 1,
                    'sound' => 'default',
                    'content-available' => 1,
                    'custom' => array(
                        'registerID' => 'teste',
                        'fromAPI' => true,
                        'action' => $actionId
                    ),
                    'payload' => array(
                        'notification' => array(
                            'action' => $actionId,
                            'title' => $text
                        ),
                    ),
        ));


        if (strlen($deviceToken) > 64) {
            $returnAndroid = \PushNotification::app('appJurAndroid')
                    ->to($deviceToken)
                    ->send($message);
        } else {
            $returnIos = \PushNotification::app('appJurIOS')
                    ->to($deviceToken)
                    ->send($message);
        }

    } catch (AdapterException $e) {
        Log::error("Push notification failure: {$e->getMessage()}");
    }
}

在我的Android App中,调用FCM委托方法时:

public void onMessageReceived(RemoteMessage remoteMessage)

remoteMessage.getData()或remoteMessage.getNotification()为null。

1 个答案:

答案 0 :(得分:0)

custompayload部分替换为datanotification

目前,您的有效负载如下所示:

{
    'badge': 1,
    'sound': 'default',
    'content-available': 1,
    'custom' : {
        'registerID': 'teste',
        'fromAPI': true,
        'action': $actionId
     },
     'payload': {
         'notification': {
             'action': $actionId,
             'title': $text
          }
     }
}

应该

{
    'badge': 1,
    'sound': 'default',
    'content_available': true,
    'data' : {
        'registerID': 'teste',
        'fromAPI': true,
        'action': $actionId
     },
     'notification': {
             'action': $actionId,
             'title': $text
     }
}

另请注意,我已将content-available替换为content_available,这是正确的格式。话虽如此,AFAIK,content_available仅用于发送给iOS客户端的有效负载。

请参阅Message TypesFCM HTTP Protocol以获取参数中的指南。