Initial plugin source code

This patchset contains the initial implementation of the
cloud-notifications plugin.

Change-Id: I833036a052b462d861f75ffd6add40a9cf5e7cd2
Signed-off-by: Jorge Ruesga <j.ruesga.criado@gmail.com>
diff --git a/BUCK b/BUCK
new file mode 100644
index 0000000..693eccc
--- /dev/null
+++ b/BUCK
@@ -0,0 +1,63 @@
+#
+# Copyright (C) 2016 Jorge Ruesga
+#
+# 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
+#
+#      http://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.
+#
+
+include_defs('//bucklets/gerrit_plugin.bucklet')
+include_defs('//bucklets/java_sources.bucklet')
+include_defs('//bucklets/maven_jar.bucklet')
+
+SOURCES = glob(['src/main/java/**/*.java'])
+RESOURCES = glob(['src/main/resources/**/*'])
+
+PROVIDED_DEPS = [
+  '//lib:gson',
+  ':h2'
+]
+
+DEPS = [
+]
+
+gerrit_plugin(
+  name = 'cloud-notifications',
+  srcs = SOURCES,
+  resources = RESOURCES,
+  manifest_entries = [
+    'Gerrit-PluginName: cloud-notifications',
+    'Gerrit-ApiType: plugin',
+    'Gerrit-ApiVersion: 2.14-SNAPSHOT',
+    'Gerrit-Module: com.ruesga.gerrit.plugins.fcm.ApiModule',
+    'Gerrit-InitStep: com.ruesga.gerrit.plugins.fcm.InitStep',
+    'Implementation-Title: Firebase Cloud Notifications Plugin',
+    'Implementation-Vendor: Jorge Ruesga',
+    'Implementation-URL: https://gerrit.googlesource.com/plugins/cloud-notifications',
+    'Implementation-Version: 2.14-SNAPSHOT'
+  ],
+  deps = DEPS,
+  provided_deps = PROVIDED_DEPS
+)
+
+java_sources(
+  name = 'cloud-notifications-sources',
+  srcs = SOURCES + RESOURCES
+)
+
+maven_jar(
+  name = 'h2',
+  id = 'com.h2database:h2:1.3.176',
+  license = 'Apache2.0',
+  exclude_java_sources = True,
+  visibility = [],
+)
+
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..11069ed
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,201 @@
+                              Apache License
+                        Version 2.0, January 2004
+                     http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+   "License" shall mean the terms and conditions for use, reproduction,
+   and distribution as defined by Sections 1 through 9 of this document.
+
+   "Licensor" shall mean the copyright owner or entity authorized by
+   the copyright owner that is granting the License.
+
+   "Legal Entity" shall mean the union of the acting entity and all
+   other entities that control, are controlled by, or are under common
+   control with that entity. For the purposes of this definition,
+   "control" means (i) the power, direct or indirect, to cause the
+   direction or management of such entity, whether by contract or
+   otherwise, or (ii) ownership of fifty percent (50%) or more of the
+   outstanding shares, or (iii) beneficial ownership of such entity.
+
+   "You" (or "Your") shall mean an individual or Legal Entity
+   exercising permissions granted by this License.
+
+   "Source" form shall mean the preferred form for making modifications,
+   including but not limited to software source code, documentation
+   source, and configuration files.
+
+   "Object" form shall mean any form resulting from mechanical
+   transformation or translation of a Source form, including but
+   not limited to compiled object code, generated documentation,
+   and conversions to other media types.
+
+   "Work" shall mean the work of authorship, whether in Source or
+   Object form, made available under the License, as indicated by a
+   copyright notice that is included in or attached to the work
+   (an example is provided in the Appendix below).
+
+   "Derivative Works" shall mean any work, whether in Source or Object
+   form, that is based on (or derived from) the Work and for which the
+   editorial revisions, annotations, elaborations, or other modifications
+   represent, as a whole, an original work of authorship. For the purposes
+   of this License, Derivative Works shall not include works that remain
+   separable from, or merely link (or bind by name) to the interfaces of,
+   the Work and Derivative Works thereof.
+
+   "Contribution" shall mean any work of authorship, including
+   the original version of the Work and any modifications or additions
+   to that Work or Derivative Works thereof, that is intentionally
+   submitted to Licensor for inclusion in the Work by the copyright owner
+   or by an individual or Legal Entity authorized to submit on behalf of
+   the copyright owner. For the purposes of this definition, "submitted"
+   means any form of electronic, verbal, or written communication sent
+   to the Licensor or its representatives, including but not limited to
+   communication on electronic mailing lists, source code control systems,
+   and issue tracking systems that are managed by, or on behalf of, the
+   Licensor for the purpose of discussing and improving the Work, but
+   excluding communication that is conspicuously marked or otherwise
+   designated in writing by the copyright owner as "Not a Contribution."
+
+   "Contributor" shall mean Licensor and any individual or Legal Entity
+   on behalf of whom a Contribution has been received by Licensor and
+   subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   copyright license to reproduce, prepare Derivative Works of,
+   publicly display, publicly perform, sublicense, and distribute the
+   Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+   this License, each Contributor hereby grants to You a perpetual,
+   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+   (except as stated in this section) patent license to make, have made,
+   use, offer to sell, sell, import, and otherwise transfer the Work,
+   where such license applies only to those patent claims licensable
+   by such Contributor that are necessarily infringed by their
+   Contribution(s) alone or by combination of their Contribution(s)
+   with the Work to which such Contribution(s) was submitted. If You
+   institute patent litigation against any entity (including a
+   cross-claim or counterclaim in a lawsuit) alleging that the Work
+   or a Contribution incorporated within the Work constitutes direct
+   or contributory patent infringement, then any patent licenses
+   granted to You under this License for that Work shall terminate
+   as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+   Work or Derivative Works thereof in any medium, with or without
+   modifications, and in Source or Object form, provided that You
+   meet the following conditions:
+
+   (a) You must give any other recipients of the Work or
+       Derivative Works a copy of this License; and
+
+   (b) You must cause any modified files to carry prominent notices
+       stating that You changed the files; and
+
+   (c) You must retain, in the Source form of any Derivative Works
+       that You distribute, all copyright, patent, trademark, and
+       attribution notices from the Source form of the Work,
+       excluding those notices that do not pertain to any part of
+       the Derivative Works; and
+
+   (d) If the Work includes a "NOTICE" text file as part of its
+       distribution, then any Derivative Works that You distribute must
+       include a readable copy of the attribution notices contained
+       within such NOTICE file, excluding those notices that do not
+       pertain to any part of the Derivative Works, in at least one
+       of the following places: within a NOTICE text file distributed
+       as part of the Derivative Works; within the Source form or
+       documentation, if provided along with the Derivative Works; or,
+       within a display generated by the Derivative Works, if and
+       wherever such third-party notices normally appear. The contents
+       of the NOTICE file are for informational purposes only and
+       do not modify the License. You may add Your own attribution
+       notices within Derivative Works that You distribute, alongside
+       or as an addendum to the NOTICE text from the Work, provided
+       that such additional attribution notices cannot be construed
+       as modifying the License.
+
+   You may add Your own copyright statement to Your modifications and
+   may provide additional or different license terms and conditions
+   for use, reproduction, or distribution of Your modifications, or
+   for any such Derivative Works as a whole, provided Your use,
+   reproduction, and distribution of the Work otherwise complies with
+   the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+   any Contribution intentionally submitted for inclusion in the Work
+   by You to the Licensor shall be under the terms and conditions of
+   this License, without any additional terms or conditions.
+   Notwithstanding the above, nothing herein shall supersede or modify
+   the terms of any separate license agreement you may have executed
+   with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+   names, trademarks, service marks, or product names of the Licensor,
+   except as required for reasonable and customary use in describing the
+   origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+   agreed to in writing, Licensor provides the Work (and each
+   Contributor provides its Contributions) on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+   implied, including, without limitation, any warranties or conditions
+   of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+   PARTICULAR PURPOSE. You are solely responsible for determining the
+   appropriateness of using or redistributing the Work and assume any
+   risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+   whether in tort (including negligence), contract, or otherwise,
+   unless required by applicable law (such as deliberate and grossly
+   negligent acts) or agreed to in writing, shall any Contributor be
+   liable to You for damages, including any direct, indirect, special,
+   incidental, or consequential damages of any character arising as a
+   result of this License or out of the use or inability to use the
+   Work (including but not limited to damages for loss of goodwill,
+   work stoppage, computer failure or malfunction, or any and all
+   other commercial damages or losses), even if such Contributor
+   has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+   the Work or Derivative Works thereof, You may choose to offer,
+   and charge a fee for, acceptance of support, warranty, indemnity,
+   or other liability obligations and/or rights consistent with this
+   License. However, in accepting such obligations, You may act only
+   on Your own behalf and on Your sole responsibility, not on behalf
+   of any other Contributor, and only if You agree to indemnify,
+   defend, and hold each Contributor harmless for any liability
+   incurred by, or claims asserted against, such Contributor by reason
+   of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+   To apply the Apache License to your work, attach the following
+   boilerplate notice, with the fields enclosed by brackets "[]"
+   replaced with your own identifying information. (Don't include
+   the brackets!)  The text should be enclosed in the appropriate
+   comment syntax for the file format. We also recommend that a
+   file or class name and description of purpose be included on the
+   same "printed page" as the copyright notice for easier
+   identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+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
+
+    http://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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8ec60ea
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+This plugin allows to obtain push event notifications through Firebase Cloud Messaging (FCM).
\ No newline at end of file
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java
new file mode 100644
index 0000000..f240abc
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm;
+
+import static com.google.gerrit.server.account.AccountResource.ACCOUNT_KIND;
+import static com.ruesga.gerrit.plugins.fcm.server.DeviceResource.DEVICE_KIND;
+import static com.ruesga.gerrit.plugins.fcm.server.TokenResource.TOKEN_KIND;
+
+import com.google.gerrit.extensions.events.AssigneeChangedListener;
+import com.google.gerrit.extensions.events.ChangeAbandonedListener;
+import com.google.gerrit.extensions.events.ChangeMergedListener;
+import com.google.gerrit.extensions.events.ChangeRestoredListener;
+import com.google.gerrit.extensions.events.ChangeRevertedListener;
+import com.google.gerrit.extensions.events.CommentAddedListener;
+import com.google.gerrit.extensions.events.DraftPublishedListener;
+import com.google.gerrit.extensions.events.HashtagsEditedListener;
+import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.gerrit.extensions.events.ReviewerAddedListener;
+import com.google.gerrit.extensions.events.ReviewerDeletedListener;
+import com.google.gerrit.extensions.events.RevisionCreatedListener;
+import com.google.gerrit.extensions.events.TopicEditedListener;
+import com.google.gerrit.extensions.registration.DynamicMap;
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.extensions.restapi.RestApiModule;
+import com.google.inject.Scopes;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.handlers.AssigneeChangedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ChangeAbandonedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ChangeMergedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ChangeRestoredEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ChangeRevertedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.CommentAddedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.DraftPublishedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.HashtagsEditedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.LifeCycleHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ReviewerAddedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.ReviewerDeletedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.RevisionCreatedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.TopicEditedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.server.DeleteToken;
+import com.ruesga.gerrit.plugins.fcm.server.Devices;
+import com.ruesga.gerrit.plugins.fcm.server.GetToken;
+import com.ruesga.gerrit.plugins.fcm.server.PostToken;
+import com.ruesga.gerrit.plugins.fcm.server.Tokens;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+
+public class ApiModule extends RestApiModule {
+
+    public static final String DEVICES_ENTRY_POINT = "devices";
+    public static final String TOKEN_ENTRY_POINT = "tokens";
+
+    @Override
+    protected void configure() {
+        bind(DatabaseManager.class).in(Scopes.SINGLETON);
+        bind(Configuration.class).in(Scopes.SINGLETON);
+        bind(FcmUploaderWorker.class).in(Scopes.SINGLETON);
+
+        // Configure listener handlers
+        DynamicSet.bind(binder(), LifecycleListener.class)
+                .to(LifeCycleHandler.class);
+        DynamicSet.bind(binder(), AssigneeChangedListener.class)
+                .to(AssigneeChangedEventHandler.class);
+        DynamicSet.bind(binder(), ChangeAbandonedListener.class)
+                .to(ChangeAbandonedEventHandler.class);
+        DynamicSet.bind(binder(), ChangeMergedListener.class)
+                .to(ChangeMergedEventHandler.class);
+        DynamicSet.bind(binder(), ChangeRestoredListener.class)
+                .to(ChangeRestoredEventHandler.class);
+        DynamicSet.bind(binder(), ChangeRevertedListener.class)
+                .to(ChangeRevertedEventHandler.class);
+        DynamicSet.bind(binder(), CommentAddedListener.class)
+                .to(CommentAddedEventHandler.class);
+        DynamicSet.bind(binder(), DraftPublishedListener.class)
+                .to(DraftPublishedEventHandler.class);
+        DynamicSet.bind(binder(), HashtagsEditedListener.class)
+                .to(HashtagsEditedEventHandler.class);
+        DynamicSet.bind(binder(), ReviewerDeletedListener.class)
+                .to(ReviewerDeletedEventHandler.class);
+        DynamicSet.bind(binder(), ReviewerAddedListener.class)
+                .to(ReviewerAddedEventHandler.class);
+        DynamicSet.bind(binder(), RevisionCreatedListener.class)
+                .to(RevisionCreatedEventHandler.class);
+        DynamicSet.bind(binder(), TopicEditedListener.class)
+                .to(TopicEditedEventHandler.class);
+
+        // Configure the Rest API
+        DynamicMap.mapOf(binder(), DEVICE_KIND);
+        DynamicMap.mapOf(binder(), TOKEN_KIND);
+        child(ACCOUNT_KIND, DEVICES_ENTRY_POINT).to(Devices.class);
+        child(DEVICE_KIND, TOKEN_ENTRY_POINT).to(Tokens.class);
+        get(TOKEN_KIND).to(GetToken.class);
+        post(DEVICE_KIND, TOKEN_ENTRY_POINT).to(PostToken.class);
+        delete(TOKEN_KIND).to(DeleteToken.class);
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/Configuration.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/Configuration.java
new file mode 100644
index 0000000..e952af3
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/Configuration.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+@Singleton
+public class Configuration {
+
+    private static final Logger log =
+            LoggerFactory.getLogger(Configuration.class);
+
+    public static final String DEFAULT_SERVER_URL =
+            "https://fcm.googleapis.com/fcm/send";
+
+    public static final String PROP_DATABASE_PATH = "databasePath";
+    public static final String PROP_SERVER_URL = "serverUrl";
+    public static final String PROP_SERVER_TOKEN = "serverToken";
+
+    public final String databasePath;
+    public final String serverToken;
+    public final String serverUrl;
+
+    @Inject
+    public Configuration(
+            @PluginName String pluginName,
+            PluginConfigFactory cfgFactory) {
+        PluginConfig cfg = cfgFactory.getFromGerritConfig(pluginName);
+        this.databasePath = cfg.getString(PROP_DATABASE_PATH);
+        this.serverToken = cfg.getString(PROP_SERVER_TOKEN);
+        String serverUrl = cfg.getString(PROP_SERVER_URL);
+        if (serverUrl == null || serverUrl.isEmpty()) {
+            serverUrl = Configuration.DEFAULT_SERVER_URL;
+        }
+        this.serverUrl = serverUrl;
+
+        if (!isEnabled()) {
+            log.info(String.format("[%s] Plugin disabled.", pluginName));
+        }
+    }
+
+    public boolean isEnabled() {
+        return this.serverToken != null && !this.serverToken.isEmpty();
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/DatabaseManager.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/DatabaseManager.java
new file mode 100644
index 0000000..9b360c8
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/DatabaseManager.java
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.h2.jdbcx.JdbcConnectionPool;
+import org.h2.jdbcx.JdbcDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+
+@Singleton
+public class DatabaseManager {
+
+    private static final Logger log =
+            LoggerFactory.getLogger(Configuration.class);
+
+    private static final String DATABASE_NAME = "cloud-notifications";
+
+    private final File dbFile;
+    private final String pluginName;
+    private final Gson gson;
+    private JdbcConnectionPool connectionPool;
+
+    @Inject
+    public DatabaseManager(
+            @PluginName String pluginName,
+            @PluginData java.nio.file.Path path,
+            Configuration cfg) {
+        this.pluginName = pluginName;
+        this.gson = new GsonBuilder().create();
+        if (cfg.databasePath != null && !cfg.databasePath.isEmpty()) {
+            this.dbFile = new File(cfg.databasePath);
+        } else {
+            this.dbFile = new File(path.toFile(), DATABASE_NAME);
+        }
+
+        boolean canWrite = (this.dbFile.exists() && this.dbFile.canWrite())
+                || (this.dbFile.getParentFile() != null
+                && this.dbFile.getParentFile().canWrite());
+        if (!canWrite) {
+            log.warn(String.format(
+                    "[%s] Database is not writeable.", pluginName));
+            throw new IllegalArgumentException("Database is not writeable: "
+                    + this.dbFile.getAbsolutePath());
+        }
+    }
+
+    public void initialize() {
+        log.info(String.format("[%s] Initialize database [%s]...",
+                pluginName, this.dbFile.getAbsolutePath()));
+
+        JdbcDataSource ds = new JdbcDataSource();
+        ds.setURL("jdbc:h2:" + this.dbFile.getAbsolutePath());
+        this.connectionPool = JdbcConnectionPool.create(ds);
+        createDatabaseIfNeeded();
+    }
+
+    public void shutdown() {
+        this.connectionPool.dispose();
+    }
+
+    public CloudNotificationInfo getCloudNotification(
+            int accountId, String deviceId, String token) {
+        Connection conn = null;
+        PreparedStatement st = null;
+        ResultSet rs = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.prepareStatement("select * from notifications where " +
+                    "user = ? and device = ? and token = ?");
+            st.setInt(1, accountId);
+            st.setString(2, deviceId);
+            st.setString(3, token);
+            rs = st.executeQuery();
+            if (rs.next()) {
+                return gson.fromJson(
+                        rs.getString("data"), CloudNotificationInfo.class);
+            }
+        } catch (SQLException ex) {
+            log.warn(String.format(
+                    "[%s] Failed to access notifications database",
+                    this.pluginName), ex);
+        } finally {
+            safelyCloseResources(conn, st, rs);
+        }
+
+        return null;
+    }
+
+    public List<CloudNotificationInfo> getCloudNotifications(int accountId) {
+        List<CloudNotificationInfo> notifications = new ArrayList<>();
+        Connection conn = null;
+        PreparedStatement st = null;
+        ResultSet rs = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.prepareStatement("select * from notifications where " +
+                    "user = ?");
+            st.setInt(1, accountId);
+            rs = st.executeQuery();
+            while (rs.next()) {
+                notifications.add(gson.fromJson(
+                        rs.getString("data"), CloudNotificationInfo.class));
+            }
+        } catch (SQLException ex) {
+            log.warn(String.format(
+                    "[%s] Failed to access notifications database",
+                    this.pluginName), ex);
+        } finally {
+            safelyCloseResources(conn, st, rs);
+        }
+
+        return notifications;
+    }
+
+    public List<CloudNotificationInfo> getCloudNotifications(
+            int accountId, String device) {
+        List<CloudNotificationInfo> notifications = new ArrayList<>();
+        Connection conn = null;
+        PreparedStatement st = null;
+        ResultSet rs = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.prepareStatement("select * from notifications where " +
+                    "user = ? and device = ?");
+            st.setInt(1, accountId);
+            st.setString(2, device);
+            rs = st.executeQuery();
+            while (rs.next()) {
+                notifications.add(gson.fromJson(
+                        rs.getString("data"), CloudNotificationInfo.class));
+            }
+        } catch (SQLException ex) {
+            log.warn(String.format(
+                    "[%s] Failed to access notifications database",
+                    this.pluginName), ex);
+        } finally {
+            safelyCloseResources(conn, st, rs);
+        }
+
+        return notifications;
+    }
+
+    public void registerCloudNotification(
+            int accountId, CloudNotificationInfo notification) {
+        Connection conn = null;
+        PreparedStatement st = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.prepareStatement("merge into notifications (user, " +
+                    "device, token, data) KEY(user, device, token) " +
+                    "VALUES (?, ?, ?, ?)");
+            st.setInt(1, accountId);
+            st.setString(2, notification.device);
+            st.setString(3, notification.token);
+            st.setString(4, gson.toJson(notification));
+            st.execute();
+        } catch (SQLException ex) {
+            log.warn(String.format(
+                    "[%s] Failed to update device: %s",
+                    this.pluginName, notification.device), ex);
+        } finally {
+            safelyCloseResources(conn, st, null);
+        }
+    }
+
+    public void unregisterCloudNotification(
+            int accountId, String deviceId, String token) {
+        Connection conn = null;
+        PreparedStatement st = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.prepareStatement("delete from notifications where " +
+                    "user = ? and device = ? and token = ?");
+            st.setInt(1, accountId);
+            st.setString(2, deviceId);
+            st.setString(3, token);
+            st.execute();
+        } catch (SQLException ex) {
+            log.warn(String.format(
+                    "[%s] Failed to delete device: %s",
+                    this.pluginName, deviceId), ex);
+        } finally {
+            safelyCloseResources(conn, st, null);
+        }
+    }
+
+    private void createDatabaseIfNeeded() {
+        Connection conn = null;
+        Statement st = null;
+        try {
+            conn = this.connectionPool.getConnection();
+            st = conn.createStatement();
+            st.execute(
+                    "create table if not exists notifications (" +
+                    "user int unsigned NOT NULL, " +
+                    "device varchar(250) NOT NULL, " +
+                    "token varchar(250) NOT NULL, " +
+                    "data varchar(4000) NOT NULL," +
+                    "primary key (user, device, token))");
+        } catch (SQLException ex) {
+            // The table exists. Ignore
+        } finally {
+            safelyCloseResources(conn, st, null);
+        }
+    }
+
+    private void safelyCloseResources(
+            Connection conn, Statement st, ResultSet rs) {
+        if (rs != null) {
+            try {
+                rs.close();
+            } catch (Exception ex) {
+                // Ignore
+            }
+        }
+        if (st != null) {
+            try {
+                st.close();
+            } catch (Exception ex) {
+                // Ignore
+            }
+        }
+        if (conn != null) {
+            try {
+                conn.close();
+            } catch (Exception ex) {
+                // Ignore
+            }
+        }
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/InitStep.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/InitStep.java
new file mode 100644
index 0000000..cffe676
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/InitStep.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm;
+
+import org.eclipse.jgit.lib.Config;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.pgm.init.api.AllProjectsConfig;
+import com.google.gerrit.pgm.init.api.ConsoleUI;
+import com.google.inject.Inject;
+
+public class InitStep implements com.google.gerrit.pgm.init.api.InitStep {
+    private final String pluginName;
+    private final ConsoleUI ui;
+    private final AllProjectsConfig allProjectsConfig;
+
+    @Inject
+    public InitStep(@PluginName String pluginName, ConsoleUI ui,
+        AllProjectsConfig allProjectsConfig) {
+      this.pluginName = pluginName;
+      this.ui = ui;
+      this.allProjectsConfig = allProjectsConfig;
+    }
+
+    @Override
+    public void run() throws Exception {
+    }
+
+    @Override
+    public void postRun() throws Exception {
+        ui.message("\n");
+        ui.header(pluginName + " Settings");
+        Config cfg = allProjectsConfig.load().getConfig();
+
+        String serverUrl = ui.readString(
+                Configuration.DEFAULT_SERVER_URL, "Firebase Server Url");
+        String serverToken = ui.readString(
+                "", "Firebase Server Token");
+        String databasePath = ui.readString(
+                "", "Database path (leave empty for default)");
+
+        if (serverUrl != null && serverUrl.isEmpty()) {
+            serverUrl = Configuration.DEFAULT_SERVER_URL;
+        }
+        cfg.setString("plugin", pluginName,
+                Configuration.PROP_SERVER_TOKEN, serverUrl);
+        if (serverToken != null && serverToken.isEmpty()) {
+            cfg.unset("plugin", pluginName, Configuration.PROP_SERVER_TOKEN);
+        } else {
+            cfg.setString("plugin", pluginName,
+                    Configuration.PROP_SERVER_TOKEN, serverToken);
+        }
+        if (databasePath != null && databasePath.isEmpty()) {
+            cfg.unset("plugin", pluginName, Configuration.PROP_DATABASE_PATH);
+        } else {
+            cfg.setString("plugin", pluginName,
+                    Configuration.PROP_DATABASE_PATH, databasePath);
+        }
+
+        allProjectsConfig.save(pluginName, pluginName + " initialized");
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/AssigneeChangedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/AssigneeChangedEventHandler.java
new file mode 100644
index 0000000..6a6b62e
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/AssigneeChangedEventHandler.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.AccountInfo;
+import com.google.gerrit.extensions.events.AssigneeChangedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gson.annotations.SerializedName;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class AssigneeChangedEventHandler extends EventHandler
+        implements AssigneeChangedListener {
+
+    private static class AssigneeInfo {
+        @SerializedName("old") public AccountInfo old;
+        @SerializedName("new") public AccountInfo _new;
+    }
+
+    @Inject
+    public AssigneeChangedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.TOPIC_CHANGED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.NEW_PATCHSETS;
+    }
+
+    @Override
+    public void onAssigneeChanged(Event event) {
+        AssigneeInfo assignee = new AssigneeInfo();
+        assignee.old = event.getOldAssignee();
+        assignee._new = event.getChange().assignee;
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(assignee);
+        notification.body = formatAccount(event.getWho())
+                + " change assignee to "
+                + formatAccount(event.getChange().assignee)
+                + " on this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeAbandonedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeAbandonedEventHandler.java
new file mode 100644
index 0000000..551980c
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeAbandonedEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.ChangeAbandonedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ChangeAbandonedEventHandler extends EventHandler
+        implements ChangeAbandonedListener {
+
+    @Inject
+    public ChangeAbandonedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.CHANGE_ABANDONED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.ABANDONED_CHANGES;
+    }
+
+    @Override
+    public void onChangeAbandoned(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " abandoned this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeMergedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeMergedEventHandler.java
new file mode 100644
index 0000000..06e6036
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeMergedEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.ChangeMergedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ChangeMergedEventHandler extends EventHandler
+        implements ChangeMergedListener {
+
+    @Inject
+    public ChangeMergedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.CHANGE_MERGED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.SUBMITTED_CHANGES;
+    }
+
+    @Override
+    public void onChangeMerged(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " merged this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRestoredEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRestoredEventHandler.java
new file mode 100644
index 0000000..8c77c1a
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRestoredEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.ChangeRestoredListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ChangeRestoredEventHandler extends EventHandler
+        implements ChangeRestoredListener {
+
+    @Inject
+    public ChangeRestoredEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.CHANGE_RESTORED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.SUBMITTED_CHANGES;
+    }
+
+    @Override
+    public void onChangeRestored(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " restored this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRevertedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRevertedEventHandler.java
new file mode 100644
index 0000000..1db50e2
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ChangeRevertedEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.ChangeRevertedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ChangeRevertedEventHandler extends EventHandler
+        implements ChangeRevertedListener {
+
+    @Inject
+    public ChangeRevertedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.CHANGE_REVERTED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.SUBMITTED_CHANGES;
+    }
+
+    @Override
+    public void onChangeReverted(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " reverted this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/CommentAddedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/CommentAddedEventHandler.java
new file mode 100644
index 0000000..32fbe51
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/CommentAddedEventHandler.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import org.apache.commons.lang.StringUtils;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.CommentAddedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class CommentAddedEventHandler extends EventHandler
+        implements CommentAddedListener {
+
+    @Inject
+    public CommentAddedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.COMMENT_ADDED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.ALL_COMMENTS;
+    }
+
+    @Override
+    public void onCommentAdded(Event event) {
+        Notification notification = createNotification(event);
+        if (event.getComment() != null) {
+            notification.extra =
+                    StringUtils.abbreviate(event.getComment(), 250);
+        }
+        notification.body = formatAccount(event.getWho())
+                + " commented on this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/DraftPublishedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/DraftPublishedEventHandler.java
new file mode 100644
index 0000000..1bab4a9
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/DraftPublishedEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.DraftPublishedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class DraftPublishedEventHandler extends EventHandler
+        implements DraftPublishedListener {
+
+    @Inject
+    public DraftPublishedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.DRAFT_PUBLISHED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.NEW_PATCHSETS;
+    }
+
+    @Override
+    public void onDraftPublished(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " published a draft on this change";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java
new file mode 100644
index 0000000..53451d6
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java
@@ -0,0 +1,258 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.api.changes.NotifyHandling;
+import com.google.gerrit.extensions.client.ReviewerState;
+import com.google.gerrit.extensions.common.AccountInfo;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.extensions.events.ChangeEvent;
+import com.google.gerrit.extensions.events.RevisionEvent;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.Predicate;
+import com.google.gerrit.server.query.QueryParseException;
+import com.google.gerrit.server.query.QueryResult;
+import com.google.gerrit.server.query.change.ChangeData;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gwtorm.server.OrmException;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public abstract class EventHandler {
+
+    static final Logger log =
+            LoggerFactory.getLogger(EventHandler.class);
+
+    private final String pluginName;
+    private final FcmUploaderWorker uploader;
+    private final AllProjectsName allProjectsName;
+    private final ChangeQueryBuilder cqb;
+    private final ChangeQueryProcessor cqp;
+    private final Provider<ReviewDb> reviewdb;
+    private final GenericFactory identifiedUserFactory;
+    private final Gson gson;
+
+    public EventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super();
+        this.pluginName = pluginName;
+        this.uploader = uploader;
+        this.allProjectsName = allProjectsName;
+        this.cqb = cqb;
+        this.cqp = cqp;
+        this.reviewdb = reviewdb;
+        this.identifiedUserFactory = identifiedUserFactory;
+        this.gson = new GsonBuilder().create();
+    }
+
+    protected abstract int getEventType();
+
+    protected abstract NotifyType getNotifyType();
+
+    protected Gson getSerializer() {
+        return this.gson;
+    }
+
+    protected Notification createNotification(ChangeEvent event) {
+        Notification notification = new Notification();
+        notification.event = getEventType();
+        notification.when = event.getWhen().getTime() / 1000L;
+        notification.who = event.getWho();
+        notification.change = event.getChange().changeId;
+        notification.legacyChangeId = event.getChange()._number;
+        notification.project = event.getChange().project;
+        notification.branch = event.getChange().branch;
+        notification.topic = event.getChange().topic;
+        notification.subject = StringUtils.abbreviate(
+                event.getChange().subject, 100);
+        if (event instanceof RevisionEvent) {
+            notification.revision =
+                    ((RevisionEvent) event).getRevision().commit.commit;
+        }
+        return notification;
+    }
+
+    protected void notify(Notification notification, ChangeEvent event) {
+        // Check if this event should be notified
+        if (event.getNotify().equals(NotifyHandling.NONE)) {
+            if (log.isDebugEnabled()) {
+                log.debug(
+                    String.format("[%s] Notify event %d is not enabled: %s",
+                        pluginName, getEventType(), gson.toJson(notification)));
+            }
+            return;
+        }
+
+        // Obtain information about the accounts that need to be
+        // notified related to this event
+        List<Integer> notifiedUsers = obtainNotifiedAccounts(event);
+        if (notifiedUsers.isEmpty()) {
+            // Nobody to notify about this event
+            return;
+        }
+
+        // Perform notification
+        if (log.isDebugEnabled()) {
+            log.debug(String.format("[%s] Sending notification %s to %s",
+                    pluginName, gson.toJson(notification),
+                    gson.toJson(notifiedUsers)));
+        }
+        this.uploader.notifyTo(notifiedUsers, notification);
+    }
+
+    private List<Integer> obtainNotifiedAccounts(ChangeEvent event) {
+        Set<Integer> notifiedUsers = new HashSet<>();
+        ChangeInfo change = event.getChange();
+        NotifyHandling notifyTo = event.getNotify();
+
+        // 1.- Owner of the change
+        notifiedUsers.add(change.owner._accountId);
+
+        // 2.- Reviewers
+        if (notifyTo.equals(NotifyHandling.OWNER_REVIEWERS) ||
+                notifyTo.equals(NotifyHandling.ALL)) {
+            if (change.reviewers != null) {
+                for (ReviewerState state : change.reviewers.keySet()) {
+                    Collection<AccountInfo> accounts =
+                            change.reviewers.get(state);
+                    for (AccountInfo account : accounts) {
+                        notifiedUsers.add(account._accountId);
+                    }
+                }
+            }
+        }
+
+        // 3.- Watchers
+        ChangeData changeData = obtainChangeData(change);
+        notifiedUsers.addAll(getWatchers(getNotifyType(), changeData));
+
+        // 4.- Remove the author of this event (he doesn't need to get
+        // the notification)
+        notifiedUsers.remove(event.getWho()._accountId);
+
+        return new ArrayList<>(notifiedUsers);
+    }
+
+    private Set<Integer> getWatchers(NotifyType type, ChangeData change) {
+        Set<Integer> watchers = new HashSet<>();
+        try {
+            for (AccountProjectWatch w : reviewdb.get().accountProjectWatches()
+                    .byProject(change.project())) {
+                add(watchers, w, type, change);
+            }
+            for (AccountProjectWatch w : reviewdb.get().accountProjectWatches()
+                    .byProject(this.allProjectsName)) {
+                add(watchers, w, type, change);
+            }
+        } catch (OrmException ex) {
+            log.error(String.format(
+                    "[%s] Failed to obtain watchers", pluginName), ex);
+        }
+        return watchers;
+    }
+
+    private boolean add(Set<Integer> watchers, AccountProjectWatch w,
+            NotifyType type, ChangeData change) throws OrmException {
+        IdentifiedUser user = identifiedUserFactory.create(w.getAccountId());
+
+        try {
+            if (filterMatch(user, w.getFilter(), change)) {
+                // If we are set to notify on this type, add the user.
+                // Otherwise, still return true to stop notifications for this user.
+                if (w.isNotify(type)) {
+                    watchers.add(w.getAccountId().get());
+                }
+                return true;
+            }
+        } catch (QueryParseException e) {
+            // Ignore broken filter expressions.
+        }
+        return false;
+    }
+
+    private boolean filterMatch(
+            CurrentUser user, String filter, ChangeData change)
+            throws OrmException, QueryParseException {
+        ChangeQueryBuilder qb = cqb.asUser(user);
+        Predicate<ChangeData> p = qb.is_visible();
+
+        if (filter != null) {
+            Predicate<ChangeData> filterPredicate = qb.parse(filter);
+            if (p == null) {
+                p = filterPredicate;
+            } else {
+                p = Predicate.and(filterPredicate, p);
+            }
+        }
+        return p == null || p.asMatchable().match(change);
+    }
+
+    private ChangeData obtainChangeData(ChangeInfo change) {
+        try {
+            QueryResult<ChangeData> changeQuery =
+                    cqp.query(cqb.parse("change:" + change._number));
+            List<ChangeData> changeQueryResults = changeQuery.entities();
+            if (changeQueryResults == null || changeQueryResults.isEmpty()) {
+                log.warn(String.format("[%s] No change found for %s",
+                        pluginName, change._number));
+                return null;
+            }
+            return changeQueryResults.get(0);
+
+        } catch (Exception ex) {
+            log.error(String.format("[%s] Failed to obtain change data: %d",
+                    pluginName, change._number), ex);
+        }
+        return null;
+    }
+
+    protected String formatAccount(AccountInfo account) {
+        if (account.name != null) {
+            return account.name;
+        }
+        if (account.username != null) {
+            return account.username;
+        }
+        return account.email;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/HashtagsEditedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/HashtagsEditedEventHandler.java
new file mode 100644
index 0000000..8c5fcc1
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/HashtagsEditedEventHandler.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.HashtagsEditedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gson.annotations.SerializedName;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class HashtagsEditedEventHandler extends EventHandler
+        implements HashtagsEditedListener {
+
+    private static class HashtagsInfo {
+        @SerializedName("removed") public String[] removed;
+        @SerializedName("added") public String[] added;
+    }
+
+    @Inject
+    public HashtagsEditedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.HASHTAG_CHANGED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.NEW_PATCHSETS;
+    }
+
+    @Override
+    public void onHashtagsEdited(Event event) {
+        HashtagsInfo hashtags = new HashtagsInfo();
+        if (event.getRemovedHashtags() != null) {
+            hashtags.removed = event.getRemovedHashtags().toArray(
+                    new String[event.getRemovedHashtags().size()]);
+        }
+        if (event.getAddedHashtags() != null) {
+            hashtags.added = event.getAddedHashtags().toArray(
+                    new String[event.getAddedHashtags().size()]);
+        }
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(hashtags);
+        notification.body = formatAccount(event.getWho())
+                + " changed change's hashtags";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/LifeCycleHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/LifeCycleHandler.java
new file mode 100644
index 0000000..eb68d5e
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/LifeCycleHandler.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.inject.Inject;
+import com.ruesga.gerrit.plugins.fcm.Configuration;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class LifeCycleHandler implements LifecycleListener {
+
+    private final DatabaseManager db;
+    private final FcmUploaderWorker uploader;
+
+    @Inject
+    public LifeCycleHandler(
+            Configuration config,
+            DatabaseManager db,
+            FcmUploaderWorker uploader) {
+        super();
+        this.db = db;
+        this.uploader = uploader;
+    }
+
+    @Override
+    public void start() {
+        this.db.initialize();
+        this.uploader.create();
+    }
+
+    @Override
+    public void stop() {
+        this.uploader.shutdown();
+        this.db.shutdown();
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerAddedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerAddedEventHandler.java
new file mode 100644
index 0000000..4f4a16d
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerAddedEventHandler.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import java.util.Arrays;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.AccountInfo;
+import com.google.gerrit.extensions.events.ReviewerAddedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ReviewerAddedEventHandler extends EventHandler
+        implements ReviewerAddedListener {
+
+    @Inject
+    public ReviewerAddedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.REVIEWER_ADDED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.ALL;
+    }
+
+    @Override
+    public void onReviewersAdded(Event event) {
+        int count = event.getReviewers().size();
+        String[] reviewers = new String[count];
+        for (int i = 0; i < count; i++) {
+            AccountInfo reviewer = event.getReviewers().get(i);
+            reviewers[i] = formatAccount(reviewer);
+        }
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(event.getReviewers());
+        notification.body = formatAccount(event.getWho())
+                + " added " + Arrays.toString(reviewers)
+                + " as reviewer on this changed";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerDeletedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerDeletedEventHandler.java
new file mode 100644
index 0000000..de5b6e0
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/ReviewerDeletedEventHandler.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.ReviewerDeletedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class ReviewerDeletedEventHandler extends EventHandler
+        implements ReviewerDeletedListener {
+
+    @Inject
+    public ReviewerDeletedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.REVIEWER_DELETED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.ALL;
+    }
+
+    @Override
+    public void onReviewerDeleted(Event event) {
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(event.getReviewer());
+        notification.body = formatAccount(event.getWho())
+                + " removed " + formatAccount(event.getReviewer())
+                + " as reviewer on this changed";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/RevisionCreatedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/RevisionCreatedEventHandler.java
new file mode 100644
index 0000000..9761542
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/RevisionCreatedEventHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.RevisionCreatedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class RevisionCreatedEventHandler extends EventHandler
+        implements RevisionCreatedListener {
+
+    @Inject
+    public RevisionCreatedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.PATCHSET_CREATED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.NEW_PATCHSETS;
+    }
+
+    @Override
+    public void onRevisionCreated(Event event) {
+        Notification notification = createNotification(event);
+        notification.body = formatAccount(event.getWho())
+                + " uploaded a new patchset";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/TopicEditedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/TopicEditedEventHandler.java
new file mode 100644
index 0000000..dfb1270
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/TopicEditedEventHandler.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.events.TopicEditedListener;
+import com.google.gerrit.reviewdb.client.AccountProjectWatch.NotifyType;
+import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gson.annotations.SerializedName;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class TopicEditedEventHandler extends EventHandler
+        implements TopicEditedListener {
+
+    private static class TopicInfo {
+        @SerializedName("old") public String old;
+    }
+
+    @Inject
+    public TopicEditedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<ReviewDb> reviewdb,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                reviewdb,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.TOPIC_CHANGED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.NEW_PATCHSETS;
+    }
+
+    @Override
+    public void onTopicEdited(Event event) {
+        TopicInfo topic = new TopicInfo();
+        topic.old = event.getOldTopic();
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(topic);
+        notification.body = formatAccount(event.getWho())
+                + " changed change's topic";
+
+        notify(notification, event);
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/messaging/Notification.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/messaging/Notification.java
new file mode 100644
index 0000000..cfe4a92
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/messaging/Notification.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.messaging;
+
+import com.google.gerrit.extensions.common.AccountInfo;
+
+public class Notification {
+    public long when;
+    public AccountInfo who;
+    public String token;
+    public int event;
+    public String change;
+    public int legacyChangeId;
+    public String revision;
+    public String project;
+    public String branch;
+    public String topic;
+    public String subject;
+    public String extra;
+
+    public transient String body;
+
+    @Override
+    public Object clone() {
+        Notification other = new Notification();
+        other.when = when;
+        other.who = who;
+        other.token = token;
+        other.event = event;
+        other.change = change;
+        other.legacyChangeId = legacyChangeId;
+        other.revision = revision;
+        other.project = project;
+        other.branch = branch;
+        other.topic = topic;
+        other.subject = subject;
+        other.extra = extra;
+        other.body = body;
+        return other;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java
new file mode 100644
index 0000000..4487788
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.rest;
+
+public class CloudNotificationEvents {
+    public static final int CHANGE_ABANDONED_EVENT = 0x01;
+    public static final int CHANGE_MERGED_EVENT = 0x02;
+    public static final int CHANGE_RESTORED_EVENT = 0x04;
+    public static final int CHANGE_REVERTED_EVENT = 0x08;
+    public static final int COMMENT_ADDED_EVENT = 0x10;
+    public static final int DRAFT_PUBLISHED_EVENT = 0x20;
+    public static final int HASHTAG_CHANGED_EVENT = 0x40;
+    public static final int REVIEWER_ADDED_EVENT = 0x80;
+    public static final int REVIEWER_DELETED_EVENT = 0x100;
+    public static final int PATCHSET_CREATED_EVENT = 0x200;
+    public static final int TOPIC_CHANGED_EVENT = 0x400;
+    public static final int ASSIGNEE_CHANGED_EVENT = 0x800;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInfo.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInfo.java
new file mode 100644
index 0000000..9086df7
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInfo.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.rest;
+
+import com.google.gson.annotations.SerializedName;
+
+public class CloudNotificationInfo {
+    /**
+     * A Firebase Cloud Messaging registered device identification.
+     */
+    @SerializedName("device") public String device;
+
+    /**
+     * A device token that unique identifies the server/account in the device.
+     */
+    @SerializedName("token") public String token;
+
+    /**
+     * When the device was registered.
+     */
+    @SerializedName("registeredOn") public String registeredOn;
+
+    /**
+     * A bitwise flag to indicate which events to notify.
+     * @see CloudNotificationEvents
+     */
+    @SerializedName("events") public int events;
+
+    /**
+     * Firebase response mode.
+     * @see CloudNotificationResponseMode
+     */
+    @SerializedName("responseMode")
+    public CloudNotificationResponseMode responseMode =
+            CloudNotificationResponseMode.BOTH;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInput.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInput.java
new file mode 100644
index 0000000..0b6ff79
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationInput.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.rest;
+
+import com.google.gson.annotations.SerializedName;
+
+public class CloudNotificationInput {
+    /**
+     * A device token that unique identifies the server/account in the device.
+     */
+    @SerializedName("token") public String token;
+
+    /**
+     * A bitwise flag to indicate which events to notify.
+     * @see CloudNotificationEvents
+     */
+    @SerializedName("events") public int events;
+
+    /**
+     * Firebase response mode.
+     * @see CloudNotificationResponseMode
+     */
+    @SerializedName("responseMode")
+    public CloudNotificationResponseMode responseMode =
+            CloudNotificationResponseMode.BOTH;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationResponseMode.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationResponseMode.java
new file mode 100644
index 0000000..ac4e900
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationResponseMode.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.rest;
+
+public enum CloudNotificationResponseMode {
+    NOTIFICATION,
+    DATA,
+    BOTH
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeleteToken.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeleteToken.java
new file mode 100644
index 0000000..b52cba3
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeleteToken.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.Response;
+import com.google.gerrit.extensions.restapi.RestModifyView;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.server.DeleteToken.Input;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class DeleteToken implements RestModifyView<TokenResource, Input> {
+
+    public static class Input {
+    }
+
+    private final Provider<CurrentUser> self;
+    private final DatabaseManager db;
+
+    @Inject
+    public DeleteToken(
+            Provider<CurrentUser> self,
+            DatabaseManager db) {
+        super();
+        this.self = self;
+        this.db = db;
+    }
+
+    @Override
+    public Response<?> apply(TokenResource rsrc, Input input)
+            throws BadRequestException {
+        // Request are only valid from the current authenticated user
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        // Delete registered client from database
+        db.unregisterCloudNotification(
+                self.get().getAccountId().get(),
+                rsrc.getDevice(), rsrc.getToken());
+
+        // Done
+        return Response.none();
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeviceResource.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeviceResource.java
new file mode 100644
index 0000000..a6cd2e7
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/DeviceResource.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.restapi.RestView;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.account.AccountResource;
+import com.google.inject.TypeLiteral;
+
+public class DeviceResource extends AccountResource {
+    public static final TypeLiteral<RestView<DeviceResource>> DEVICE_KIND =
+            new TypeLiteral<RestView<DeviceResource>>() {};
+
+    private final String device;
+
+    public DeviceResource(IdentifiedUser user, String device) {
+      super(user);
+      this.device = device;
+    }
+
+    public String getDevice() {
+      return device;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Devices.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Devices.java
new file mode 100644
index 0000000..5a0cf74
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Devices.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.registration.DynamicMap;
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.ChildCollection;
+import com.google.gerrit.extensions.restapi.IdString;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
+import com.google.gerrit.extensions.restapi.RestView;
+import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.account.AccountResource;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+public class Devices
+        implements ChildCollection<AccountResource, DeviceResource> {
+
+    private final DynamicMap<RestView<DeviceResource>> views;
+    private final ListDevices list;
+    private final Provider<CurrentUser> self;
+
+    @Inject
+    public Devices(
+            DynamicMap<RestView<DeviceResource>> views,
+            ListDevices list,
+            Provider<CurrentUser> self) {
+        super();
+        this.views = views;
+        this.list = list;
+        this.self = self;
+    }
+
+    @Override
+    public RestView<AccountResource> list()
+            throws ResourceNotFoundException, AuthException {
+        return this.list;
+    }
+
+    @Override
+    public DeviceResource parse(AccountResource rsrc, IdString id)
+            throws ResourceNotFoundException, Exception {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new ResourceNotFoundException();
+        }
+
+        return new DeviceResource(rsrc.getUser(), id.get());
+    }
+
+    @Override
+    public DynamicMap<RestView<DeviceResource>> views() {
+        return this.views;
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetDevice.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetDevice.java
new file mode 100644
index 0000000..e4113e6
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetDevice.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class GetDevice implements RestReadView<DeviceResource> {
+
+    private final Provider<CurrentUser> self;
+
+    @Inject
+    public GetDevice(Provider<CurrentUser> self) {
+        super();
+        this.self = self;
+    }
+
+    @Override
+    public CloudNotificationInfo apply(DeviceResource rsrc)
+            throws BadRequestException, ResourceNotFoundException {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        throw new BadRequestException("unsupported!");
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetToken.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetToken.java
new file mode 100644
index 0000000..0f9883c
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/GetToken.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class GetToken implements RestReadView<TokenResource> {
+
+    private final Provider<CurrentUser> self;
+    private final DatabaseManager db;
+
+    @Inject
+    public GetToken(
+            Provider<CurrentUser> self,
+            DatabaseManager db) {
+        super();
+        this.self = self;
+        this.db = db;
+    }
+
+    @Override
+    public CloudNotificationInfo apply(TokenResource rsrc)
+            throws BadRequestException, ResourceNotFoundException {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        // Obtain from database
+        CloudNotificationInfo notification = db.getCloudNotification(
+                self.get().getAccountId().get(),
+                rsrc.getDevice(), rsrc.getToken());
+        if (notification == null) {
+            throw new ResourceNotFoundException();
+        }
+        return notification;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListDevices.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListDevices.java
new file mode 100644
index 0000000..aca2b6b
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListDevices.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import java.util.List;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.account.AccountResource;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class ListDevices implements RestReadView<AccountResource> {
+    private final Provider<CurrentUser> self;
+
+    @Inject
+    public ListDevices(Provider<CurrentUser> self) {
+        super();
+        this.self = self;
+    }
+
+    @Override
+    public List<DeviceResource> apply(AccountResource rsrc)
+            throws BadRequestException {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        // Since return devices information from account, can lead to
+        // tokens leak to different apps, just avoid it. Just return
+        // empty information.
+        throw new BadRequestException("unsupported!");
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListTokens.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListTokens.java
new file mode 100644
index 0000000..159f8aa
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/ListTokens.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import java.util.List;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+
+@Singleton
+public class ListTokens implements RestReadView<DeviceResource> {
+
+    private final Provider<CurrentUser> self;
+    private final DatabaseManager db;
+
+    @Inject
+    public ListTokens(
+            Provider<CurrentUser> self,
+            DatabaseManager db) {
+        super();
+        this.self = self;
+        this.db = db;
+    }
+
+    @Override
+    public List<CloudNotificationInfo> apply(DeviceResource rsrc)
+            throws BadRequestException {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        // Obtain the list of tokens for the device
+        return db.getCloudNotifications(
+                self.get().getAccountId().get(), rsrc.getDevice());
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/PostToken.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/PostToken.java
new file mode 100644
index 0000000..b2492a0
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/PostToken.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.RestModifyView;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInput;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class PostToken
+        implements RestModifyView<DeviceResource, CloudNotificationInput> {
+
+    private final Provider<CurrentUser> self;
+    private final DatabaseManager db;
+    private final SimpleDateFormat formatter;
+
+    @Inject
+    public PostToken(
+            Provider<CurrentUser> self,
+            DatabaseManager db) {
+        super();
+        this.self = self;
+        this.db = db;
+
+        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
+        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
+    }
+
+    @Override
+    public CloudNotificationInfo apply(
+            DeviceResource rsrc, CloudNotificationInput input)
+            throws BadRequestException {
+        // Request are only valid from the current authenticated user
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new BadRequestException("invalid account!");
+        }
+
+        // Check request parameters
+        if (input.token == null || input.token.isEmpty()) {
+            throw new BadRequestException("token is empty!");
+        }
+
+        final String registeredOn;
+        synchronized (formatter) {
+            registeredOn = formatter.format(new Date());
+        }
+
+        // Create or update the notification
+        CloudNotificationInfo notification = db.getCloudNotification(
+                self.get().getAccountId().get(),
+                rsrc.getDevice(), input.token);
+        if (notification == null) {
+            notification = new CloudNotificationInfo();
+            notification.device = rsrc.getDevice();
+            notification.token = input.token;
+        }
+        notification.registeredOn = registeredOn;
+        notification.events = input.events;
+        notification.responseMode = input.responseMode;
+
+        // Persist the notification
+        db.registerCloudNotification(
+                self.get().getAccountId().get(), notification);
+
+        return notification;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/TokenResource.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/TokenResource.java
new file mode 100644
index 0000000..336343f
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/TokenResource.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.restapi.RestView;
+import com.google.inject.TypeLiteral;
+
+public class TokenResource extends DeviceResource {
+    public static final TypeLiteral<RestView<TokenResource>> TOKEN_KIND =
+            new TypeLiteral<RestView<TokenResource>>() {};
+
+    private final String token;
+
+    public TokenResource(DeviceResource rsrc, String token) {
+      super(rsrc.getUser(), rsrc.getDevice());
+      this.token = token;
+    }
+
+    public String getToken() {
+        return token;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Tokens.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Tokens.java
new file mode 100644
index 0000000..edb4d77
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/server/Tokens.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.server;
+
+import com.google.gerrit.extensions.registration.DynamicMap;
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.ChildCollection;
+import com.google.gerrit.extensions.restapi.IdString;
+import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
+import com.google.gerrit.extensions.restapi.RestView;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+public class Tokens
+    implements ChildCollection<DeviceResource, TokenResource> {
+
+    private final DynamicMap<RestView<TokenResource>> views;
+    private final ListTokens list;
+    private final Provider<CurrentUser> self;
+
+    @Inject
+    public Tokens(
+            DynamicMap<RestView<TokenResource>> views,
+            ListTokens list,
+            Provider<CurrentUser> self) {
+        super();
+        this.views = views;
+        this.list = list;
+        this.self = self;
+    }
+
+    @Override
+    public RestView<DeviceResource> list()
+            throws ResourceNotFoundException, AuthException {
+        return this.list;
+    }
+
+    @Override
+    public TokenResource parse(DeviceResource rsrc, IdString id)
+            throws ResourceNotFoundException, Exception {
+        if (self.get() == null || self.get() != rsrc.getUser()) {
+            throw new ResourceNotFoundException();
+        }
+
+        return new TokenResource(rsrc, id.get());
+    }
+
+    @Override
+    public DynamicMap<RestView<TokenResource>> views() {
+        return this.views;
+    }
+
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestInfo.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestInfo.java
new file mode 100644
index 0000000..4aeb401
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestInfo.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.workers;
+
+import com.google.gson.annotations.SerializedName;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+
+public class FcmRequestInfo {
+    @SerializedName("to") public String to;
+    @SerializedName("time_to_live") public Integer timeToLive;
+    @SerializedName("notification") public FcmRequestNotificationInfo notification;
+    @SerializedName("data") public Notification data;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestNotificationInfo.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestNotificationInfo.java
new file mode 100644
index 0000000..458e49e
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmRequestNotificationInfo.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.workers;
+
+import com.google.gson.annotations.SerializedName;
+
+public class FcmRequestNotificationInfo {
+    @SerializedName("title") public String title;
+    @SerializedName("body") public String body;
+    @SerializedName("icon") public String icon = "ic_stats_gerrit_notification";
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseInfo.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseInfo.java
new file mode 100644
index 0000000..f93c23a
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseInfo.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.workers;
+
+import java.util.List;
+
+import com.google.gson.annotations.SerializedName;
+
+public class FcmResponseInfo {
+    @SerializedName("multicast_id") public Long multicastId;
+    @SerializedName("success") public Integer success;
+    @SerializedName("failure") public Integer failure;
+    @SerializedName("canonical_ids") public Integer canonicalIds;
+    @SerializedName("results") public List<FcmResponseResultInfo> results;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseResultInfo.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseResultInfo.java
new file mode 100644
index 0000000..fe34eb5
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmResponseResultInfo.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.workers;
+
+import com.google.gson.annotations.SerializedName;
+
+public class FcmResponseResultInfo {
+    @SerializedName("message_id") public String messageId;
+    @SerializedName("error") public String error;
+    @SerializedName("registration_id") public String registrationId;
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmUploaderWorker.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmUploaderWorker.java
new file mode 100644
index 0000000..c4b00ab
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/workers/FcmUploaderWorker.java
@@ -0,0 +1,286 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * 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
+ *
+ *      http://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.
+ */
+package com.ruesga.gerrit.plugins.fcm.workers;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.ruesga.gerrit.plugins.fcm.Configuration;
+import com.ruesga.gerrit.plugins.fcm.DatabaseManager;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationInfo;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationResponseMode;
+
+@Singleton
+public class FcmUploaderWorker {
+
+    private static final Logger log =
+            LoggerFactory.getLogger(FcmUploaderWorker.class);
+
+    private static class SubmitNotification {
+        int accountId;
+        String device;
+        String token;
+        FcmRequestInfo request;
+        int attempt;
+    }
+
+    private final String pluginName;
+    private final Configuration config;
+    private final DatabaseManager db;
+    private final Gson gson;
+    private ExecutorService executor;
+    private ScheduledExecutorService delayedExecutor;
+
+    @Inject
+    public FcmUploaderWorker(
+            @PluginName String pluginName,
+            Configuration config,
+            DatabaseManager db) {
+        super();
+        this.pluginName = pluginName;
+        this.config = config;
+        this.db = db;
+        this.gson = new GsonBuilder().create();
+    }
+
+    public void create() {
+        this.executor = Executors.newCachedThreadPool();
+        this.delayedExecutor = Executors.newScheduledThreadPool(50);
+    }
+
+    public void shutdown() {
+        this.executor.shutdown();
+        this.delayedExecutor.shutdownNow();
+    }
+
+    public void notifyTo(final List<Integer> notifiedAccounts,
+            final Notification notification) {
+        if (!config.isEnabled()) {
+            return;
+        }
+
+        for (final Integer accountId : notifiedAccounts) {
+            this.executor.submit(new Runnable() {
+                @Override
+                public void run() {
+                    asyncNotify(accountId, notification);
+                }
+            });
+        }
+    }
+
+    private void asyncNotify(int accountId, Notification notification) {
+        List<CloudNotificationInfo> notifications =
+                db.getCloudNotifications(accountId);
+        for (CloudNotificationInfo to : notifications) {
+            if ((notification.event | to.events) == to.events) {
+                Notification what = (Notification) notification.clone();
+                what.token = to.token;
+
+                sendNotification(createRequest(accountId, to, what));
+            }
+        }
+    }
+
+    private synchronized void sendNotification(SubmitNotification submit) {
+        try {
+            String data = gson.toJson(submit.request);
+            if (log.isDebugEnabled()) {
+                log.debug(String.format(
+                        "[%] Sending fcm notification: %s", pluginName, data));
+            }
+
+            URL url = new URL(config.serverUrl);
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            conn.setDoOutput(true);
+            conn.setRequestMethod("POST");
+            conn.setRequestProperty(
+                    "Content-Type", "application/json");
+            conn.setRequestProperty(
+                    "Authorization", "key=" + config.serverToken);
+            conn.setRequestProperty(
+                    "Content-Length", Integer.toString(data.length()));
+
+            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
+            try {
+                os.write(data.getBytes());
+                os.flush();
+            } finally {
+                try {
+                    os.close();
+                } catch (IOException ex) {
+                    // Ignore
+                }
+            }
+
+            int responseCode = conn.getResponseCode();
+            if (responseCode == 200) {
+                StringBuilder response = new StringBuilder();
+                BufferedReader in = new BufferedReader(
+                        new InputStreamReader(conn.getInputStream()));
+                try {
+                    String inputLine;
+                    while ((inputLine = in.readLine()) != null) {
+                        response.append(inputLine);
+                    }
+                } finally {
+                    try {
+                        in.close();
+                    } catch (IOException ex) {
+                        // Ignore
+                    }
+                }
+
+                // Process the server response
+                processResponse(conn, submit, gson.fromJson(
+                        response.toString(), FcmResponseInfo.class));
+
+            } else if (responseCode == 500) {
+                // Retry
+                retryAfter(conn, submit);
+
+            } else {
+                log.warn(String.format(
+                        "[%s] Failed to send notification to device %s. code: %d",
+                            pluginName, submit.request.to, responseCode));
+            }
+
+        } catch (Throwable e) {
+            log.warn(String.format(
+                    "[%s] Failed to send notification to device %s",
+                        pluginName, submit.request.to), e);
+        }
+    }
+
+    private SubmitNotification createRequest(
+            int accountId, CloudNotificationInfo to, Notification what) {
+        FcmRequestInfo request = new FcmRequestInfo();
+        request.to = to.device;
+        request.timeToLive = 28800; // 8 hours
+        if (to.responseMode.equals(CloudNotificationResponseMode.NOTIFICATION)
+                || to.responseMode.equals(CloudNotificationResponseMode.BOTH)) {
+            request.notification = new FcmRequestNotificationInfo();
+            request.notification.title = "Gerrit notification";
+            request.notification.body = what.body;
+        }
+        if (to.responseMode.equals(CloudNotificationResponseMode.DATA)
+                || to.responseMode.equals(CloudNotificationResponseMode.BOTH)) {
+            request.data = what;
+        }
+
+        SubmitNotification submit = new SubmitNotification();
+        submit.accountId = accountId;
+        submit.device = to.device;
+        submit.token = to.token;
+        submit.request = request;
+        return submit;
+    }
+
+    private void processResponse(HttpURLConnection conn,
+            SubmitNotification submit, FcmResponseInfo response) {
+        if (response.failure > 0 && !response.results.isEmpty()) {
+            FcmResponseResultInfo result = response.results.get(0);
+            if (result.error != null) {
+                switch (result.error) {
+                case "Unavailable":
+                case "InternalServerError":
+                    // Retry
+                    retryAfter(conn, submit);
+                    break;
+
+                case "NotRegistered":
+                    // Remove this client from the database
+                    if (log.isDebugEnabled()) {
+                        log.debug("[%] %d - %s - %s is not registered. " +
+                                "Remove from db.",
+                                pluginName, submit.accountId,
+                                submit.device,
+                                submit.token);
+                    }
+                    db.unregisterCloudNotification(
+                            submit.accountId,
+                            submit.device,
+                            submit.token);
+                    break;
+
+                case "DeviceMessageRateExceeded":
+                    // TODO we should stop sending messages to this device
+                    // or we will get banned. This shouldn't happen
+                    // normally. Need to thought how to handle this.
+                    break;
+
+                default:
+                    break;
+                }
+            }
+        }
+
+        // The message was successfully sent
+    }
+
+    private void retryAfter(
+            HttpURLConnection conn, final SubmitNotification submit) {
+        submit.attempt++;
+
+        // Is Retry-After header present?
+        int retryAfter = 0;
+        try {
+            Map<String, List<String>> headers = conn.getHeaderFields();
+            if (headers.containsKey("Retry-After")) {
+                retryAfter = Integer.parseInt(
+                        headers.get("Retry-After").get(0));
+            }
+        } catch (Exception ex) {
+            // Ignore
+        }
+        if (retryAfter == 0) {
+            // If Retry-After isn't present, then use our
+            // own exponential back-off timeout (in seconds)
+            retryAfter = submit.attempt * 30;
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("[%] Retry fcm notification to %s after %d seconds",
+                    pluginName, submit.request.to, retryAfter);
+        }
+        this.delayedExecutor.schedule(new Runnable() {
+            @Override
+            public void run() {
+                sendNotification(submit);
+            }
+        }, retryAfter, TimeUnit.SECONDS);
+    }
+
+}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
new file mode 100644
index 0000000..619f0df
--- /dev/null
+++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1,8 @@
+About
+=====
+
+This is a Gerrit's plugin to send notifications using Firebase Cloud Messaging
+to devices previously registered.
+
+Checkout the document fcm.md in this same folder, for an explanation about
+how it works.
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..1bc2410
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,26 @@
+Build
+=====
+
+This plugin is built with Buck.
+
+Build in Gerrit tree
+--------------------
+
+Issue this commands to build the plugin inside the Gerrit's source tree:
+
+```
+  git clone https://gerrit.googlesource.com/gerrit
+  cd gerrit
+  git submodule init
+  git submodule update
+  git clone https://gerrit.googlesource.com/plugins/cloud-notifications
+  buck build plugins/cloud-notifications
+```
+
+The output is created in
+
+```
+  buck-out/gen/plugins/cloud-notifications/cloud-notifications.jar
+```
+
+Check out the Gerrit Plugin API [documentation](https://gerrit-review.googlesource.com/Documentation/dev-buck.html#_extension_and_plugin_api_jar_files)
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
new file mode 100644
index 0000000..f807f6f
--- /dev/null
+++ b/src/main/resources/Documentation/config.md
@@ -0,0 +1,31 @@
+Configuration
+=============
+
+This plugin provides a wizard configuration that will you ask
+for plugin configuration parameters on install.
+
+Manual configuration
+--------------------
+
+You can manual configure the plugin by adding the plugin properties
+described below in this document in the gerrit.config file.
+
+```
+[plugin "cloud-notifications"]
+        serverUrl = https://fcm.googleapis.com/fcm/send
+        serverToken = <SERVER_API_KEY>
+        databasePath = <DATABASE_LOCATION_PATH>
+```
+
+Plugin parameters
+--------------------
+
+* serverUrl: The Firebase Cloud Messaging backend url.
+Default: https://fcm.googleapis.com/fcm/send
+
+* serverToken: Your Firebase Cloud Messaging server token. You can
+grab it from your Firebase project console, in Configuration > Cloud
+Messaging > Server key
+
+* databasePath: The path to where to store the plugin database. Leave
+empty to use the default path ($gerrit/data/cloud-notifications/cloud-notifications.h2.db)
diff --git a/src/main/resources/Documentation/fcm.md b/src/main/resources/Documentation/fcm.md
new file mode 100644
index 0000000..dce5199
--- /dev/null
+++ b/src/main/resources/Documentation/fcm.md
@@ -0,0 +1,226 @@
+Firebase Cloud Notifications Plugin
+===================================
+
+App implementations should register the device token identifier received from FCM, using the *Register Cloud Notification* method to send the device token identifier and configure how it wants to be notified. When registering the device, it also should provide a token, which is returned on every notification, that allows to identify
+the Gerrit instance how send the notification. In addition, an app can configure which events wants to listen to and how the message from FCM is received (as notification, as custom data or received both). Read the FCM documentation to know which is more appropriated for your device operation system. See *Custom Data* in the FCM Notification section to see the information returned as custom data.
+
+A client application can unregistered a device (and stop receiving notifications) from the Gerrit instance using the *Unregister Cloud Notification* method.
+
+Client applications must use a combintion of fcm device identifier + a unique local account token, so they can register to listen notification for different accounts on this Gerrit instance. Token must 
+
+
+REST API
+--------
+
+This plugin addes new methods to the /accounts Gerrit REST Api entry point to allow a device to register/unregister to receive event notification in this gerrit instance.
+
+***
+
+**Get Cloud Notifications**
+
+`'GET /accounts/{account-id}/devices/{device-id}/tokens'`
+
+Retrieves a list of registered tokens by a device hold by the Gerrit server instance.
+
+*Request*
+This request requires an authenticated call and only returns information if account-id is the authenticated account. This method returns a list of *CloudNotificationInfo* entities (see below).
+
+    GET /accounts/self/devices/bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1/tokens
+
+*Response*
+
+    HTTP1.1 200 OK
+    Content-Disposition: attachment
+    Content-Type: application/json; charset=UTF-8
+    
+    )]}'
+    [
+     {
+       "device": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1",
+       "token": "f986567456f107d0eb2d84c85ac5aed2",
+       "registeredOn": "2016-11-25 14:45:03.123",
+       "events": 1023,
+       "responseMode": "DATA"
+     }
+    ]
+
+***
+
+**Get Cloud Notification**
+
+`'GET /accounts/{account-id}/devices/{device-id}/tokens/{token}'`
+
+Retrieves a registered device information hold by the Gerrit server instance.
+
+*Request*
+This request requires an authenticated call and only returns information if account-id is the authenticated account. This method returns a *CloudNotificationInfo* entity (see below).
+
+    GET /accounts/self/devices/bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1/tokens/f986567456f107d0eb2d84c85ac5aed2
+
+*Response*
+
+    HTTP1.1 200 OK
+    Content-Disposition: attachment
+    Content-Type: application/json; charset=UTF-8
+    
+    )]}'
+    {
+      "device": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1",
+      "token": "f986567456f107d0eb2d84c85ac5aed2",
+      "registeredOn": "2016-11-25 14:45:03.123",
+      "events": 1023,
+      "responseMode": "DATA"
+    }
+
+***
+
+**Register Cloud Notification**
+
+`'POST /accounts/{account-id}/devices/{device-id}/tokens'`
+
+Register or update a registered device information to be hold by the Gerrit server instance.
+
+*Request*
+This request requires an authenticated call and is only valid if account-id is the authenticated account. This method accepts a *CloudNotificationInput* entity (see below).
+
+    POST /accounts/self/devices/bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1/tokens
+    Content-Type: application/json
+    
+    {
+      "token": "f986567456f107d0eb2d84c85ac5aed2",
+      "events": 8,
+      "responseMode": "NOTIFICATION"
+    }
+
+As a response, this method returs the registered *CloudNotificationInfo* entity (see below).
+
+*Response*
+
+    HTTP1.1 200 OK
+    Content-Disposition: attachment
+    Content-Type: application/json; charset=UTF-8
+    
+    )]}'
+    {
+      "deviceId": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1",
+      "token": "f986567456f107d0eb2d84c85ac5aed2",
+      "registeredOn": "2016-11-25 14:45:03.123",
+      "events": 8,
+      "responseMode": "NOTIFICATION"
+    }
+
+***
+
+**Unregister Cloud Notification**
+
+`'DELETE /accounts/{account-id}/devices/{device-id}/tokens/{token}'`
+
+Unregister a registered device information hold by the Gerrit server instance.
+
+*Request*
+This request requires an authenticated call and is only valid if account-id is the authenticated account.
+
+    DELETE /accounts/self/devices/bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1/tokens/f986567456f107d0eb2d84c85ac5aed2
+
+*Response*
+
+    HTTP/1.1 204 No Content
+
+***
+
+**CloudNotificationInfo**
+
+Entity with information about a registered device.
+
+`device: A Firebase Cloud Messaging registered device identification.`
+
+`token: A device token that unique identifies the server/account in the device.`
+
+`registeredOn: When the device was registered.`
+
+`events : A bitwise flag to indicate which events to notify. See CloudNotificationEvents below.`
+
+`responseMode: Firebase response mode. See CloudNotificationResponseMode below.`
+
+***
+
+**CloudNotificationInput**
+
+Entity with information about a device to be register.
+
+`token: A device token that unique identifies the server/account in the device.`
+
+`events : A bitwise flag to indicate which events to notify. See CloudNotificationEvents below.`
+
+`responseMode: Firebase response mode. See CloudNotificationResponseMode below.`
+
+***
+
+**CloudNotificationEvents**
+
+Enumeration of available events to notify to the client device.
+
+`CHANGE_ABANDONED_EVENT = 0x01`
+
+`CHANGE_MERGED_EVENT = 0x02`
+
+`CHANGE_RESTORED_EVENT = 0x04`
+
+`COMMENT_ADDED_EVENT = 0x08`
+
+`DRAFT_PUBLISHED_EVENT = 0x10`
+
+`HASHTAG_CHANGED_EVENT = 0x20`
+
+`REVIEWER_ADDED_EVENT = 0x40`
+
+`REVIEWER_DELETED_EVENT = 0x80`
+
+`PATCHSET_CREATED_EVENT = 0x100`
+
+`TOPIC_CHANGED_EVENT = 0x200`
+
+***
+
+**CloudNotificationResponseMode**
+
+Enumeration of available Firebase notification modes.
+
+`NOTIFICATION: Notification in the device is handled by Firebase`
+
+`DATA: Notification in the device is handled by the app which receives a custom object data`
+
+`BOTH: Notification includes both: notification data and custom object data`
+
+
+
+FCM NOTIFICATION
+----------------
+
+**Custom Data**
+
+This is the information sent as a custom data inside the FCM notification. Depends on the event, some of this fields could be empty.
+
+`when: An unix timestamp on when notification was created`
+
+`who: A json AccountInfo object of the account that originated the notification`
+
+`token: The token used to registered the device`
+
+`event: The event type (see CloudNotificationEvents above)`
+
+`change: The change identifier`
+
+`legacyChangeId: The legacy change identifier`
+
+`revision: The revision/patchset identifier`
+
+`project: The project identifier`
+
+`branch: The branch identifier`
+
+`topic: The topic identifier`
+
+`subject: The subject of the change`
+
+`extra: Extra notification information, if present. The structure depends on event type.`