【问题标题】:"registration_ids" field cannot be empty (laravel and Firebase)“registration_ids”字段不能为空(laravel 和 Firebase)
【发布时间】:2020-12-30 10:03:04
【问题描述】:

我遇到了 Firebase“registration_ids”的问题。我想从我的 laravel 网站向移动设备发送推送通知,当我在视图中发送通知时出现错误:

“registration_ids”字段不能为空

这是我的刀片脚本:

firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();  

function initFirebaseMessagingRegistration() {

        messaging
        .requestPermission()
        .then(function () {
            return messaging.getToken()
        })
        .then(function(token) {
            console.log(token);

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });  

            $.ajax({
                url: '{{ route("save-token") }}',
                type: 'POST',
                data: {
                    token: token
                },
                dataType: 'JSON',
                success: function (response) {
                    alert('Token saved successfully.');
                },
                error: function (err) {
                    console.log('User Chat Token Error'+ err);
                },
            });
  
        }).catch(function (err) {
            console.log('User Chat Token Error'+ err);
        });
 }        

messaging.onMessage(function(payload) {
    const noteTitle = payload.notification.title;
    const noteOptions = {
        body: payload.notification.body,
        icon: payload.notification.icon,
    };

    new Notification(noteTitle, noteOptions);
});

这是我的控制器:

public function saveToken(Request $request)
{
    auth()->user()->update(['device_token'=>$request->token]);
    return response()->json(['token saved successfully.']);
}  

/**
 * Write code on Method
 *
 * @return response()
 */

public function sendNotification(Request $request)
{
    $firebaseToken = User::whereNotNull('device_token')->pluck('device_token')->all();

    $SERVER_API_KEY = 'mine';

    $data = [
        "registration_ids" => $firebaseToken,
        "notification" => [
            "title" => $request->title,
            "body" => $request->body,  
        ]
    ];

    $dataString = json_encode($data);

    $headers = [
        'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);

    $response = curl_exec($ch);

    dd($response);
}

我无法解决问题。 有人可以帮我吗?

【问题讨论】:

  • 看起来User::whereNotNull('device_token')->pluck('device_token')->all(); 没有返回任何结果。我们无法说明为什么这是基于共享的代码。
  • @FrankvanPuffelen 我能做什么?

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


【解决方案1】:

请更新您在用户表中的设备令牌

【讨论】:

    【解决方案2】:

    这是因为device_token 列是空的。

    User::whereNotNull('device_token')->pluck('device_token')->all()
    

    它返回0,因此registration_ids字段不能为空。

    【讨论】:

      【解决方案3】:
      1. 将这两行添加到routes/web.php
          use App\Http\Controllers\HomeController;
          Route::get('/home', [HomeController::class, 'index'])->name('home');
      
      1. 在 firebase 中,进入 Project Settings->General->Your apps,然后添加 WEB (SDK)。除其他外,将生成一个代码示例。从那里复制开始的部分:“apiKey:...”并以“measurementId:...”结束

      2. 将第 2 步中的值粘贴到 resources/views/home.blade.php 第 44 行

      3. 将步骤 2 中的值粘贴到 public/firebase-messaging-sw.js 的第 11 行

        再试一下网址:http://127.0.0.1:8000/login

      source

      【讨论】:

      • 感谢@Abilogos
      猜你喜欢
      • 2016-11-20
      • 2022-08-20
      • 2021-08-24
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2017-12-21
      相关资源
      最近更新 更多