【问题标题】:FCM Payload null in Laravel production APILaravel 生产 API 中的 FCM Payload null
【发布时间】:2018-05-06 02:11:12
【问题描述】:

我有一个 Laravel API,它通过 FCM 使用 laravel-push-notifications 框架发送推送通知。在我的本地主机中,我收到带有数据有效负载的消息,但在生产模式下,相同的代码在接收消息时返回有效负载 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 应用中,调用 FCM 委托方法时:

public void onMessageReceived(RemoteMessage remoteMessage)

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

【问题讨论】:

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


    【解决方案1】:

    将您的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

    【讨论】:

      猜你喜欢
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多