Tôi muốn gửi thông báo đẩy để tất cả người dùng máy tính để bàn của tôi sử dụng tin nhắn google cloudDesktop push thông báo bằng tin nhắn điện toán đám mây của Google và nhân viên dịch vụ
tôi hoàn thành các bước sau thành công
- khởi tạo trong lao động dịch vụ
- Đã tạo một dự án trên Bảng điều khiển dành cho nhà phát triển của Google
- Đã thêm tệp kê khai
- được gửi bằng cách sử dụng php CURL
Đây là CURL tôi lệnh
$url = 'https://android.googleapis.com/gcm/send';
$msg="hi";
$data = array('title'=> $msg,'message'=>'Hello');
$regids= // added regids;
$fields = array('to' => $regids,'data' => $data);
$headers = array('Authorization: My auth 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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
print_r(json_encode($fields));
$result=curl_exec($ch);
echo $result;
if($result==FALSE)
{
die('Curl Failed');
}
curl_close($ch);
Tất cả mọi thứ tốt đẹp đang làm việc và tôi có thể hiển thị một thông báo mặc định sử dụng đoạn mã sau
self.addEventListener('push', function(event) {
console.log('Push message', event);
var title = 'Push message';
event.waitUntil(
self.registration.showNotification(title, {
body: 'The Message',
icon: 'images/icon.png',
tag: 'my-tag'
}));
});
Nhưng những gì tôi cần là để hiển thị các thông báo tin nhắn mà tôi đã gửi qua lệnh CURL (Trong ứng dụng chúng tôi có thể thực hiện điều đó một cách dễ dàng)
Tôi nhận được mã sau để nhận thông báo đẩy (g oogle)
self.addEventListener('push', function(event) {
// Since there is no payload data with the first version
// of push messages, we'll grab some data from
// an API and use it to populate a notification
event.waitUntil(
fetch(SOME_API_ENDPOINT).then(function(response) {
if (response.status !== 200) {
// Either show a message to the user explaining the error
// or enter a generic message and handle the
// onnotificationclick event to direct the user to a web page
console.log('Looks like there was a problem. Status Code: ' + response.status);
throw new Error();
}
// Examine the text in the response
return response.json().then(function(data) {
if (data.error || !data.notification) {
console.error('The API returned an error.', data.error);
throw new Error();
}
var title = data.notification.title;
var message = data.notification.message;
var icon = data.notification.icon;
var notificationTag = data.notification.tag;
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag
});
});
}).catch(function(err) {
console.error('Unable to retrieve data', err);
var title = 'An error occurred';
var message = 'We were unable to get the information for this push message';
var icon = URL_TO_DEFAULT_ICON;
var notificationTag = 'notification-error';
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag
});
})
);
});
Nó luôn luôn hiển thị
Chúng tôi không thể để có được những thông tin này nhắn push
gì là SOME_API_ENDPOINT đề cập trong mã đó?
Tôi đã thử với https://android.googleapis.com/gcm/send thay vì điểm cuối và cũng với điểm cuối người dùng trong nhân viên dịch vụ nhưng không hoạt động.
Bất kỳ trợ giúp được nhiều đánh giá
Tôi nghĩ bạn không thể gửi tin nhắn văn bản bằng cách sử dụng thông báo chrome desktop bạn có thể thêm trình xử lý sự kiện để nhấn và sau đó khi bạn nhấn fetch ('url của bạn ở đây') '. bạn có thể kiểm tra mã mẫu tại đây: https://jsfiddle.net/saineshmamgain/j4L2jfns/ – CodeZilla
cũng SOME_API_ENDPOINT ở đây đề xuất một dịch vụ web sẽ trả về hình ảnh tin nhắn, tiêu đề, v.v. Có thể trên máy chủ của bạn. bạn có thể chuyển một số id duy nhất trong url mà bạn có thể xác định trang web trong trường hợp có nhiều trang web. bạn có thể quản lý tin nhắn từ chương trình phụ trợ. – CodeZilla
@Codezilla: Cảm ơn, tôi sẽ kiểm tra xem – Sree