Exemplo n.º 1
0
 public static function sendNotification($title, $body, $icon, $url, $post)
 {
     require_once plugin_dir_path(__FILE__) . 'web-push.php';
     $webPush = new WebPush();
     update_option('webpush_payload', array('title' => html_entity_decode($title, ENT_COMPAT, get_option('blog_charset')), 'body' => html_entity_decode($body, ENT_COMPAT, get_option('blog_charset')), 'icon' => $icon, 'url' => $url, 'postID' => $post ? $post->ID : ''));
     $gcmKey = get_option('webpush_gcm_key');
     $webPush->setGCMKey($gcmKey);
     $webPush->setVAPIDInfo(get_option('webpush_vapid_key'), get_option('webpush_vapid_audience'), get_option('webpush_vapid_subject'));
     $notification_count = 0;
     // Sending notifications could take some time, so we extend the maximum
     // execution time for the script.
     set_time_limit(120);
     $subscriptions = WebPush_DB::get_subscriptions();
     $subscription_num = count($subscriptions);
     foreach ($subscriptions as $subscription) {
         // Ignore GCM endpoints if we don't have a GCM key.
         $isGCM = strpos($subscription->endpoint, GCM_REQUEST_URL) === 0;
         if (!$gcmKey && $isGCM) {
             continue;
         }
         $webPush->addRecipient($subscription->endpoint, function ($success) use($subscription, &$notification_count) {
             if (!$success) {
                 // If there's an error while sending the push notification,
                 // the subscription is no longer valid, hence we remove it.
                 WebPush_DB::remove_subscription($subscription->endpoint);
             } else {
                 $notification_count++;
             }
         });
     }
     $webPush->sendNotifications();
     return $notification_count;
 }
 function send_multiple_notifications_success_with_VAPID($forceWP)
 {
     $oldNum = getSentNotificationNum();
     $webPush = new WebPush($forceWP);
     $webPush->setGCMKey('aKey');
     $webPush->setVAPIDInfo(file_get_contents('tests/example_ec_key_with_public_key.pem'), 'https://example.org', 'mailto:webpush_ops@catfacts.example.com');
     $self = $this;
     $webPush->addRecipient('https://android.googleapis.com/gcm/send/endpoint', function ($success) use($self) {
         $self->assertTrue($success);
     });
     $webPush->addRecipient('http://localhost:55555/201' . (USE_VAPID ? '//vapid' : ''), function ($success) use($self) {
         $self->assertTrue($success);
     });
     $webPush->addRecipient('http://localhost:55555/200' . (USE_VAPID ? '//vapid' : ''), function ($success) use($self) {
         $self->assertTrue($success);
     });
     $webPush->requests[0]['url'] = 'http://localhost:55555/200/gcm';
     $webPush->sendNotifications();
     $this->assertEquals($oldNum + 3, getSentNotificationNum());
 }