2017-06-07 8 views
0

我想用PHP發送推送通知,但我不知道該怎麼做。用PHP推送通知。如何使用它?

我成功創建了服務工作者。

我從這個頁面拿了鑰匙https://web-push-codelab.appspot.com/。但我不明白他們爲什麼給一個私鑰?

然後我測試我的推notifiaction,併發送一些通知從這個頁面。 但我想從我的PHP請求發送推送通知。 更多的,我會在所有bowsers可能的工作,這段代碼只適用於谷歌瀏覽器。我們可以用firefox,safari,opéra做到這一點嗎?

我已經在使用librairie web-push-php了。 (avaible這裏:https://github.com/web-push-libs/web-push-php) 我複製/ PASE從文檔中的代碼,我remplace我的鑰匙,而訊息:`

<?php 
    error_reporting(E_ALL); 
    ini_set('display_errors',1); 
    use Minishlink\WebPush\WebPush; 


    $notifications = array(
     array(
      'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+ 
      'payload' => 'hello !', 
      'userPublicKey' => '*****', // base 64 encoded, should be 88 chars 
      'userAuthToken' => '*****', // base 64 encoded, should be 24 chars 
     ), array(
      'endpoint' => 'https://android.googleapis.com/gcm/send/abcdef...', // Chrome 
      'payload' => '{msg:"je fais un test"}', 
      'userPublicKey' => '*****', 
      'userAuthToken' => null, 
     ) 
    ); 

    $webPush = new WebPush(); 

    // send multiple notifications with payload 
    foreach ($notifications as $notification) { 
     $webPush->sendNotification(
      $notification['endpoint'], 
      $notification['payload'], // optional (defaults null) 
      $notification['userPublicKey'], // optional (defaults null) 
      $notification['userAuthToken'] // optional (defaults null) 
     ); 
    } 
    $webPush->flush(); 

But this code don't work, the page return this : { 「multicast_id」:6971447349404987967, 「成功」:0,「失敗「:1,」canonical_ids「:0,」results「:[{」error「:」InvalidRegistration「}]}'

我也創建了一個firebase帳戶,但我不知道如何使用它。

而且我還想定位通知。 我想發送像「xxx評論到你的帖子」或「xxx喜歡你的帖子」或「xxx發給你的朋友請求」的通知

希望,你可以幫助我。

+0

您剛剛在公共論壇上發佈了您的公鑰和身份驗證令牌。您應該考慮您的帳戶被盜用,並立即撤銷該令牌。 –

+0

可能的重複https://stackoverflow.com/questions/29705993/using-gcm-to-send-notifications-on-app-returns-invalidregistration-error –

回答

1

我從這個頁面拿了一把鑰匙https://web-push-codelab.appspot.com/。但 我不明白他們爲什麼給一個私鑰?

服務器應該生成一個公鑰/私鑰對。公共密鑰由瀏覽器用來創建訂閱。私鑰保留在服務器上,用於加密推送消息。

而且我還想定位通知。我想送 像「XXX曾評論到您的文章」或「XXX喜歡 您的文章」或「XXX發送給你一個好友請求」

在某些時候你必須在瀏覽器通知註冊通過致電pushManager.subscribe訂閱(但是您的問題缺少此代碼)。這將返回一個可解析爲訂閱的承諾。您應該將此訂閱與當前經過身份驗證的用戶相關聯。例如,您可以使用AJAX將訂閱發送到您的服務器,並將其與當前通過身份驗證的用戶一起存儲。無論何時您想通知此用戶,您都可以使用與此用戶關聯的訂閱。

您可能想看看https://web-push-book.gauntface.com/