“registration_ids”字段不能为空(laravel 和 Firebase)

Posted

技术标签:

【中文标题】“registration_ids”字段不能为空(laravel 和 Firebase)【英文标题】:"registration_ids" field cannot be empty (laravel and 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 我能做什么? 【参考方案1】:

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

【讨论】:

【参考方案2】:

这是因为device_token 列是空的。

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

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

【讨论】:

【参考方案3】:
    将这两行添加到routes/web.php
    use App\Http\Controllers\HomeController;
    Route::get('/home', [HomeController::class, 'index'])->name('home');

    在 firebase 中,进入 Project Settings->General->Your apps,然后添加 WEB (SDK)。除其他外,将生成一个代码示例。从那里复制开始的部分:“apiKey:...”并以“measurementId:...”结束

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

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

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

source

【讨论】:

感谢@Abilogos

以上是关于“registration_ids”字段不能为空(laravel 和 Firebase)的主要内容,如果未能解决你的问题,请参考以下文章

Django:表单中的电子邮件字段返回“此字段不能为空/此字段不能为空”,即使其中有地址

asp怎样检查字段是不是为空?

在sqlserver2000中,如果把字段改为不能为空?

java hibernate mariaDB 列“字段”不能为空

网格部件判断字段内容不能为空

MessageEmbed 字段值不能为空