firebase,无法接收通知(客户端)

时间:2017-03-26 16:22:27

标签: javascript firebase notifications messaging

我从firebase服务器接收推送通知时遇到问题

来自firebase-messaging-sw.js文件的代码(用于接收)

// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase     libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': 'xxxxxxxxx'
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};

return self.registration.showNotification(notificationTitle,
notificationOptions);
});
// [END background_handler]

// [START initialize_firebase_in_sw] // Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here, other Firebase libraries // are not available in the service worker. importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js'); // Initialize the Firebase app in the service worker by passing in the // messagingSenderId. firebase.initializeApp({ 'messagingSenderId': 'xxxxxxxxx' }); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. const messaging = firebase.messaging(); // [END initialize_firebase_in_sw] // If you would like to customize notifications that are received in the // background (Web app is closed or not in browser focus) then you should // implement this optional method. // [START background_handler] messaging.setBackgroundMessageHandler(function(payload) { console.log('[firebase-messaging-sw.js] Received background message ', payload); // Customize notification here const notificationTitle = 'Background Message Title'; const notificationOptions = { body: 'Background Message body.', icon: '/firebase-logo.png' }; return self.registration.showNotification(notificationTitle, notificationOptions); }); // [END background_handler]

订阅代码工作100%罚款,所以我不会显示它(我需要更多的声誉来添加超过2个链接)

我使用服务器端代码发送通知:

  define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxxxxx');
     $url = 'https://fcm.googleapis.com/fcm/send';
        $priority="high";
        $notification= array('title' => 'Some title','body' => 'hi', "priority" =>"high", );

    $tokens = array('Token-1','token-2');


        $fields = array(
             'registration_ids' => $tokens,
             'notification' => $notification

            );


        $headers = array(
            'Authorization:key='.API_ACCESS_KEY,
            'Content-Type: application/json'
            );

       $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));
        // echo json_encode($fields);
       $result = curl_exec($ch);           
       echo curl_error($ch);
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       echo  $result;

服务器端也可以正常工作,因为向firebase发送通知,服务器返回此响应:

{ “multicast_id”:7642532958945472490, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “MESSAGE_ID”: “0:1490541716147780%e609af1cf9fd7ecd”}]}

如您所见,通知已成功发送。

但是我无法收到它,我没有看到chrome中的通知窗口。

所有来源都来自github firebase的例子。

有什么问题?为什么我收不到通知?标识符和访问密钥是正确的,javascript中没有错误或警告,这可以通过成功发送通知来证明

P.S我使用pastebin,因为我无法在stackoverflow上格式化代码段,抱歉。

感谢

1 个答案:

答案 0 :(得分:0)

首先,我建议您使用postman之类的东西来测试请求。因此,您可以保证问题不是由您的代码引起的。

我。您是否调用此函数来指定firebase消息传递的服务工作者(从您获取注册令牌的页面)?

messaging.useServiceWorker(swRegistration);

II。 firebase示例仅使用apiKey在服务工作者内部进行初始化。这给我带来了一些问题。相反,我正在使用页面中也使用的配置:

var config = {
   apiKey: "xxxaSyDxxxaQMQW67JxxxnUzxxxSEhVxxxeqKXI",
   authDomain: "xxx.firebaseapp.com",
   databaseURL: "https://xxx.firebaseio.com",
   projectId: "xxx-xxx-xxx",
   storageBucket: "xxx-xxx-xxx.apxxxot.com",
   messagingSenderId: "2xxxx5xxx"
};

firebase.initializeApp(config);