首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >firebase,无法接收通知(客户端)

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

Stack Overflow用户
提问于 2017-03-27 00:22:27
回答 1查看 871关注 0票数 0

我在接收来自firebase服务器的推送通知时遇到问题

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

代码语言:javascript
复制
// [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个链接)

下面是我用来发送通知的服务器端代码:

代码语言:javascript
复制
  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,"results":{"message_id":"0:1490541716147780%e609af1cf9fd7ecd"}}

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

但是我收不到它,我在chrome中看不到通知窗口。

所有源代码都取自github的firebase示例。

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

另外,我使用的是pastebin,因为我不能格式化stackoverflow上的代码部分,抱歉。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-01-15 19:15:59

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

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

代码语言:javascript
复制
messaging.useServiceWorker(swRegistration);

二、firebase示例仅使用apiKey在服务工作线程中进行初始化。这给我带来了一些问题。相反,我使用的是配置,该配置也在页面中使用:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43031331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档