簡體   English   中英

Laravel 推送通知與 Firebase

[英]Laravel Push Notification with Firebase

我想使用 firebase 將推送通知從 laravel 發送到 android 客戶端

我怎樣才能做到這一點?

這是我的 firebase sdk:

<script>

var firebaseConfig = {
apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
authDomain: "XXXXXXXXXXXXXXXXXXXXXXX",
projectId: "XXXXXXXXXXXXXXXXXXXXX",
storageBucket: "XXXXXXXXXXXXXXXXXXXXx",
messagingSenderId: "XXXXXXXXXXXXXXXXXXXXXX",
appId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
measurementId: "XXXXXXXXXXXXXXXXX"
};



firebase.initializeApp(firebaseConfig);

</script>

使用 firebase 將推送通知從 laravel 發送到 android 客戶端的簡單方法是 ZF6E57C9DE709E455FEB0D95531

這是我用 FCM 發送通知的代碼。

$url = 'https://fcm.googleapis.com/fcm/send';

    /*api_key available in: Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key*/
    $api_key = 'AAAAr...W34:....a5ogzkm3F62';

    $fields = '{
        "to": "/topics/dongeng",
        "collapse_key": "type_a",
        "notification": {
            "body": "Yuk Tonton Dongengnya",
            "title": "Ada Dongeng Baru : '+$title+ '"
        },
        "data": {
            "body": "Yuk Tonton Dongengnya"",
            "title": "Ada Dongeng Baru : ' + $title + '",
            "link": "'+$link+'"
        }
    }';

    //header includes Content type and api key
    $headers = array(
        'Content-Type:application/json',
        'Authorization:key=' . $api_key
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);

    return $result;

https://techvblogs.com/blog/firebase-push-notification-laravel

https://web-tuts.com/laravel-firebase-push-notification-example/

來自附加帖子的完整示例的代碼。 我希望這將有所幫助。

// Your web app's Firebase configuration
    var firebaseConfig = {
        apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
        authDomain: "XXXXXXX.firebaseapp.com",
        projectId: "XXXXXXXXXX",
        storageBucket: "XXXXXXXXXX.appspot.com",
        messagingSenderId: "XXXXXXXXXX",
        appId: "1:XXXXXXXXX:web:XXXXXXXXXXXXX"
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);

    const messaging = firebase.messaging();

    function initFirebaseMessagingRegistration() {
        messaging.requestPermission().then(function () {
            return messaging.getToken()
        }).then(function(token) {
            
            axios.post("{{ route('fcmToken') }}",{
                _method:"PATCH",
                token
            }).then(({data})=>{
                console.log(data)
            }).catch(({response:{data}})=>{
                console.error(data)
            })

        }).catch(function (err) {
            console.log(`Token Error :: ${err}`);
        });
    }

    initFirebaseMessagingRegistration();
  
    messaging.onMessage(function({data:{body,title}}){
        new Notification(title, {body});
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM