2016-05-20 5 views
0

Google Chromeのプッシュ通知を使用して、ユーザーにプッシュ通知を送信しています。 私はこのチュートリアルのようにそれを作った: https://developers.google.com/web/fundamentals/getting-started/push-notifications/Googleクロムプッシュ通知キャッシュ

それが働いていますが、私は「sw.js」ファイルの「メッセージ」を変更する問題があります。それはimadiatlyを変えていないし、プッシュメッセージを送信したユーザーは以前のメッセージを受け取る。

お願いします。

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

をあなたがそうのようなデータを送信しようとしているように見えます:

$fields = array(
    'registration_ids' => $registrationIDs, 
    'data' => array("message" => $message), 
); 

これは間違っていますウェブプッシュとの関係。データ属性はGCMに固有であり、Chromeではサポートされていません。データを送信するには、ペイロードを暗号化する必要があります。チェックアウト:https://github.com/web-push-libs/web-push-php

第2に、サービスワーカーのshowNotification()コードを変更してこのデータを使用する必要があります。 (console.log(event.data)に電話してください)

関連する問題