我正在使用谷歌浏览器推送通知向我的用户发送推送通知。 我做了这个教程: https://developers.google.com/web/fundamentals/getting-started/push-notifications/
它正常工作,但是当我更改“sw.js”文件中的“message”时出现问题。它并没有瞬间改变,当我发送推送消息时,用户得到了上一条消息。
请帮帮我。
sw.js:
/*
*
* Push Notifications codelab
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*
*/
// Version 0.1
'use strict';
console.log('Started', self);
self.addEventListener('install', function(event) {
self.skipWaiting();
console.log('Installed', event);
});
self.addEventListener('activate', function(event) {
console.log('Activated', event);
});
self.addEventListener('push', function(event) {
console.log('Push ', event);
var title = 'Head';
event.waitUntil(
self.registration.showNotification(title, {
'body': 'Body!',
'icon': 'push/images/hergunyeni-push.png'
}));
});
self.addEventListener('notificationclick', function(event) {
console.log('Notification click: tag', event.notification.tag);
// Android doesn't close the notification when you click it
// See http://crbug.com/463146
event.notification.close();
var url = 'https://www.hergunyeni.com/tum-urunler.html?sort=p.date_added&order=DESC&utm_source=Push&utm_medium=push&utm_campaign=Yeniler&utm_content=Yeniler';
// Check if there's already a tab open with this URL.
// If yes: focus on the tab.
// If no: open a tab with the URL.
event.waitUntil(
clients.matchAll({
type: 'window'
})
.then(function(windowClients) {
console.log('WindowClients', windowClients);
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
console.log('WindowClient', client);
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});
推送发送php文件:
//if ($_GET["pass"] == 'SxJYd4tZTp8pAttxiSeywnes26Jb4gGxjPU1q1q3HBm7bJ4ovE') {
// Replace with the real server API key from Google APIs
$apiKey = "mykey";
// ofis büyük bilgisayar
$registrationIDs = array( 'eQrvphoh3Kk:APA91bFQyqOX_xd3YF5pF4aXORJaUbdG61GMcwD7w-7ZYQpagiLoF9xzotjfBZv0yWC9oQTgrFpZuQWBURH_kRXGxOL-tjnLHohrtu38u4CVOIeDpdSkHo1NzZEfSHsHh8pVhfmDK_0n','ctsHPe87NgY:APA91bFIFwtfQfCmG0Np0BrHh2eXioE1vlElBd3SoQZs6vCBLf4aikvj5VtHf8J-ueh0QqQSBBQ5z-O5n2Kraqz3Gcuqc9FUASzh7gUHXEasC1gi_l7fn8e4pUa41lNoG-eK8BPPOSiz');
// Message to be sent
$message = "hi Shailesh";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the URL, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
//}
答案 0 :(得分:0)
看起来你正试图像这样发送数据:
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
这是错误与网络推送相关的事情。数据属性特定于GCM,Chrome不支持。要发送数据,您需要加密有效负载。也许请查看:https://github.com/web-push-libs/web-push-php
其次,您需要更改服务工作者中的showNotification()代码以使用此数据。 (尝试拨打console.log(event.data)
)