Browse Source

Firebase Push notification integration for the official client

pull/978/head
Timothée Jaussoin 5 years ago
parent
commit
a382293746
  1. 1
      CHANGELOG.md
  2. 1
      app/Configuration.php
  3. 8
      app/helpers/UtilsHelper.php
  4. 26
      app/widgets/AdminMain/adminmain.tpl
  5. 6
      app/widgets/AdminMain/locales.ini
  6. 21
      app/widgets/Notification/Notification.php
  7. 7
      app/widgets/Presence/Presence.php
  8. 32
      src/Movim/Firebase.php

1
CHANGELOG.md

@ -37,6 +37,7 @@ v0.19 (trunk)
* TLS encryption enabling is non blocking during connection
* Send the Presence a bit earlier on login
* Add a "copy to clipboard" button in Preview
* Firebase Push notification integration for the official client
v0.18
---------------------------

1
app/Configuration.php

@ -12,6 +12,7 @@ class Configuration extends Model
public $fillable = [
'description',
'firebaseauthorizationkey',
'info',
'unregister',
'gifapikey',

8
app/helpers/UtilsHelper.php

@ -606,7 +606,7 @@ define('DEFAULT_HTTP_USER_AGENT', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http
/*
* @desc Request a simple url
*/
function requestURL(string $url, int $timeout = 10, $post = false, bool $json = false)
function requestURL(string $url, int $timeout = 10, $post = false, bool $json = false, array $headers = [])
{
$ch = curl_init($url);
@ -618,10 +618,12 @@ function requestURL(string $url, int $timeout = 10, $post = false, bool $json =
curl_setopt($ch, CURLOPT_USERAGENT, DEFAULT_HTTP_USER_AGENT);
if ($json) {
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
array_push($headers, 'Accept: application/json');
}
if (is_array($post)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}

26
app/widgets/AdminMain/adminmain.tpl

@ -181,6 +181,32 @@
</li>
</ul>
<h3>{$c->__('firebase.title')}</h3>
<div>
<textarea type="text" name="firebaseauthorizationkey" id="firebaseauthorizationkey"
placeholder="FirebaseKey"
onclick="MovimUtils.textareaAutoheight(this);"
oninput="MovimUtils.textareaAutoheight(this);"/>{$conf->firebaseauthorizationkey}</textarea>
<label for="info">{$c->__('firebase.label')}</label>
</div>
<ul class="list thick">
<li>
<span class="primary icon bubble color blue">
<i class="material-icons">notifications_active</i>
</span>
<div>
<p>{$c->__('firebase.info1')}</p>
<p>
<a href="https://console.firebase.google.com/" target="_blank">
{$c->__('firebase.info2')}
</a>
</p>
</div>
</li>
</ul>
<br />
<h3>{$c->__('credentials.title')}</h3>

6
app/widgets/AdminMain/locales.ini

@ -47,6 +47,12 @@ label = Yout Tenor API Key
info1 = Movim integrates the Tenor API to allow GIF search and publication in Chat
info2 = Access Tenor and get your API key
[firebase]
title = Firebase Integration
label = Your Firebase Authorization Key
info1 = Movim can send Push Notifications to the Movim Android app using Firebase
info2 = Go to the Firebase panel to configure and generate your Authorization key
[log]
empty = Empty
syslog = Syslog

21
app/widgets/Notification/Notification.php

@ -1,9 +1,12 @@
<?php
use Movim\Firebase;
use Movim\Widget\Base;
use Movim\RPC;
use Movim\Session;
use App\Configuration;
class Notification extends Base
{
public static $rpcCall = null;
@ -75,7 +78,23 @@ class Notification extends Base
if ($group != null) {
$action = $group;
}
RPC::call('Notification.android', $title, $body, $picture, $action);
$s = new Session;
$firebaseToken = $s->get('firebasetoken');
// We have Firebase enabled
if ($firebaseToken) {
$configuration = Configuration::get();
$firebaseKey = $configuration->firebaseauthorizationkey;
if ($firebaseKey) {
$fb = new Firebase($firebaseKey, $firebaseToken);
$fb->notify($title, $body, $picture, $action);
}
} else {
// We try to deliver it trough the WebSocket
RPC::call('Notification.android', $title, $body, $picture, $action);
}
}
if (array_key_exists($first, $notifs)) {

7
app/widgets/Presence/Presence.php

@ -12,6 +12,7 @@ use Moxl\Xec\Action\PubsubSubscription\Get as GetPubsubSubscriptions;
use Moxl\Stanza\Stream;
use Movim\Daemon\Session;
use Movim\Session as MovimSession;
class Presence extends Base
{
@ -67,6 +68,12 @@ class Presence extends Base
$this->onSessionUp();
}
public function ajaxSetFireBaseToken(string $token)
{
$s = new MovimSession;
$s->set('firebasetoken', $token);
}
public function ajaxAskLogout()
{
$view = $this->tpl();

32
src/Movim/Firebase.php

@ -0,0 +1,32 @@
<?php
namespace Movim;
class Firebase
{
private $_key;
private $_token;
public function __construct(string $key, string $token)
{
$this->_key = $key;
$this->_token = $token;
}
public function notify(string $title, string $body = null, string $image = null, string $action = null)
{
$headers = ['Authorization:key='.$this->_key,'Content-Type:application/json'];
$fields = [
'to' => $this->_token,
'data' => [
'title' => $title,
'body' => $body,
'image' => $image,
'action' => $action
]
];
var_dump(requestURL('https://fcm.googleapis.com/fcm/send', 10, json_encode($fields), true, $headers));
}
}
Loading…
Cancel
Save