简体   繁体   English

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

[英]firebase,can not receive notifications(client side)

I have problems with receiving push notifications from firebase server 我从Firebase服务器接收推送通知时遇到问题

There is code from firebase-messaging-sw.js file(which uses for receiving) 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] 

Subscribe code work 100% fine ,so i will not show it( i need more reputation to add more than 2 links) 订阅代码可以100%正常工作,所以我不会显示它(我需要更多信誉才能添加2个以上链接)

There is the server side code Which I use to send notifications: 有用于发送通知的服务器端代码:

  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; 

server side also works fine ,because sending notification to firebase ,server returns this response : 服务器端也可以正常工作,因为向firebase发送通知,服务器返回以下响应:

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

as you see ,notification sent successfully. 如您所见,通知已成功发送。

But i cannot receive it ,i dont see notification window in chrome. 但是我无法收到它,我没有在Chrome中看到通知窗口。

All sources are taken from github examples of firebase. 所有资源均取自firebase的github示例。

What is the problem ? 问题是什么 ? Why i can't receive notification? 为什么我无法收到通知? Identifier and access key are correct,no errors or warning in javascript, this is proved by the fact that the notification is sent SUCCESSFULLY 标识符和访问密钥正确,JavaScript中没有错误或警告,这是通过通知已成功发送的事实证明的

PS I uses pastebin ,because i can't format code section on stackoverflow,sorry. PS我使用pastebin,因为抱歉,我无法格式化stackoverflow上的代码部分。

thanks 谢谢

At first of all, I recommend you to use something like postman for testing requests. 首先,我建议您使用邮递员之类的东西来测试请求。 Thereby you can guarantee that the issue is not caused by your code. 因此,您可以保证问题不是由您的代码引起的。

i. 一世。 Do you call this function to specify the service worker for firebase messaging (from your page where you get your registration token)? 您是否调用此函数来指定用于Firebase消息传递的服务工作者(从您获得注册令牌的页面)?

messaging.useServiceWorker(swRegistration);

ii. II。 The firebase example uses only the apiKey for initialization inside the service worker. firebase示例仅使用apiKey来在service worker中进行初始化。 This caused some problems for me. 这给我带来了一些问题。 Instead I'm using the config which is also used in the page: 相反,我使用的是在页面中也使用的配置:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM