[Git][noosfero/noosfero][master] 2 commits: Fix Push Notification Plugin

Tallys Martins gitlab at mg.gitlab.com
Thu May 4 21:13:30 BRT 2017


Tallys Martins pushed to branch master at Noosfero / noosfero


Commits:
a8aca1c5 by Tallys Martins at 2017-05-04T20:56:36-03:00
Fix Push Notification Plugin

- Updates plugin README and cloud message gem to use the new messaging service,
FCM

Signed-off-by: Dylan Guedes <djmgguedes at gmail.com>
Signed-off-by: Sabryna Sousa <sabryna.sousa1323 at gmail.com>
Signed-off-by: Tallys Martins <tallysmartins at gmail.com>

- - - - -
dddd8f13 by Tallys Martins at 2017-05-05T00:13:15+00:00
Merge branch 'push_notification_fixes' into 'master'

Fix Push Notification Plugin

See merge request !1099
- - - - -


4 changed files:

- plugins/push_notification/Gemfile
- plugins/push_notification/README
- plugins/push_notification/lib/push_notification_helper.rb
- plugins/push_notification/test/unit/push_notification_helper_test.rb


Changes:

=====================================
plugins/push_notification/Gemfile
=====================================
--- a/plugins/push_notification/Gemfile
+++ b/plugins/push_notification/Gemfile
@@ -1 +1 @@
-gem "gcm"
+gem "fcm"


=====================================
plugins/push_notification/README
=====================================
--- a/plugins/push_notification/README
+++ b/plugins/push_notification/README
@@ -1,4 +1,4 @@
-README - GCM - Google Cloud Message Plugin
+README - Push Notification - Firebase Cloud Message Plugin
 ==========================================
 
 This plugin enables push notifications for mobile platforms
@@ -10,11 +10,11 @@ INSTALL
 Enable Plugin
 -------------
 
-You need to enable GCM Plugin on your Noosfero:
+You need to enable Push Notification Plugin on your Noosfero:
 
 cd <your_noosfero_dir>
-./script/noosfero-plugins install gcm
-./script/noosfero-plugins enable gcm
+./script/noosfero-plugins install push_notification
+./script/noosfero-plugins enable push_notification
 
 Activation and Plugin Configuration
 -----------------------------------
@@ -22,24 +22,24 @@ Activation and Plugin Configuration
 As a Noosfero administrator user, go to administrator panel:
 
 - Click on "Enable/disable plugins" option
-- Click on "Google Cloud Message Plugin" check-box
+- Click on "Push Notification Plugin" check-box
 - Click on "Configurations" just below the previous step
-- *Fill in the form with your server API key registered for GCM
-- Select from the check-boxes the notifications you want GCM plugin to send.
+- *Fill in the form with your server API key registered for FCM
+- Select from the check-boxes the notifications you want FCM plugin to send.
 
-*API key can be obtained creating a project in the GCM console, needed when preparing the application to use push notification.
+*API key can be obtained creating a project in the FCM console, needed when preparing the application to use push notification.
 Link below:
 
 - https://console.developers.google.com/start
 
 More information can be found in the following links:
-- https://developers.google.com/cloud-messaging/gcm
+- https://developers.google.com/cloud-messaging/fcm
 - https://developers.google.com/cloud-messaging/server
 - https://developers.google.com/cloud-messaging/registration
 - https://developers.google.com/instance-id/
 
-After that, the mobile application need to take care of device registration to GCM and send the device token
-to GCM plugin through noosfero API. There are endpoints to add, see and remove device tokens.
+After that, the mobile application need to take care of device registration to FCM and send the device token
+to FCM plugin through noosfero API. There are endpoints to add, see and remove device tokens.
 
 - post request to /api/v1/push_notification_plugin/device_tokens with "tokens" parameter like token1,token2,token3
 will register the tokens token1, token2 and token3 to the api current logged user, defined by noosfero's private token


=====================================
plugins/push_notification/lib/push_notification_helper.rb
=====================================
--- a/plugins/push_notification/lib/push_notification_helper.rb
+++ b/plugins/push_notification/lib/push_notification_helper.rb
@@ -1,24 +1,24 @@
 module PushNotificationHelper
 
-  def gcm_instance
-    api_key = settings[:server_api_key]
-    gcm = GCM.new(api_key)
-    gcm
+  def fcm_instance
+    api_key = plugin_settings.settings[:server_api_key]
+    fcm = FCM.new(api_key)
+    fcm
   end
 
-  def settings
-    return Noosfero::Plugin::Settings.new(environment, PushNotificationPlugin.class)
+  def plugin_settings
+    return Noosfero::Plugin::Settings.new(environment, PushNotificationPlugin)
   end
 
-  #data should be a hash, like {some_info: 123123}
+  #data should be a hash inside `data` attribute, like {some_info: 123123}
   def send_to_users(flag, users, data)
     return false unless users.present?
     users |= subscribers_additional_users(flag, users.first.environment)
     users = filter_users_for_flag(flag, users)
     return false unless users.present?
     tokens = tokens_for_users(users)
-    gcm = gcm_instance
-    response = gcm.send(tokens, data)
+    fcm = fcm_instance
+    response = fcm.send(tokens, {data: data})
     response[:response]
   end
 


=====================================
plugins/push_notification/test/unit/push_notification_helper_test.rb
=====================================
--- a/plugins/push_notification/test/unit/push_notification_helper_test.rb
+++ b/plugins/push_notification/test/unit/push_notification_helper_test.rb
@@ -4,13 +4,26 @@ require 'test_helper'
 class PushNotificationHelperTest < ActiveSupport::TestCase
   include  PushNotificationHelper
 
+  def setup
+    @environment = Environment.default
+  end
+  attr_reader :environment
+
+  should 'get FCM instance with api key' do
+    data = {:server_api_key => "mykey"}
+    settings = Noosfero::Plugin::Settings.new(environment, PushNotificationPlugin, data)
+    settings.save!
+
+    assert_equal "mykey", fcm_instance.api_key
+  end
+
   should 'get all tokens for a group of users' do
-    user = User.create!(:login => 'homer', :email => 'homer at example.com', :password => 'beer', :password_confirmation => 'beer', :environment => Environment.default)
+    user = User.create!(:login => 'homer', :email => 'homer at example.com', :password => 'beer', :password_confirmation => 'beer', :environment => environment)
     user.activate
     PushNotificationPlugin::DeviceToken.create!(:token => "tokenHomer1", device_name: "my device", :user => user)
     PushNotificationPlugin::DeviceToken.create!(:token => "tokenHomer2", device_name: "my device", :user => user)
 
-    user2 = User.create!(:login => 'bart', :email => 'bart at example.com', :password => 'fart', :password_confirmation => 'fart', :environment => Environment.default)
+    user2 = User.create!(:login => 'bart', :email => 'bart at example.com', :password => 'fart', :password_confirmation => 'fart', :environment => environment)
     user2.activate
     PushNotificationPlugin::DeviceToken.create!(:token => "tokenBart1", device_name: "my device", :user => user2)
     PushNotificationPlugin::DeviceToken.create!(:token => "tokenBart2", device_name: "my device", :user => user2)
@@ -22,9 +35,9 @@ class PushNotificationHelperTest < ActiveSupport::TestCase
   end
 
   should 'filter users registered for a notification' do
-    user = User.create!(:login => 'homer', :email => 'homer at example.com', :password => 'beer', :password_confirmation => 'beer', :environment => Environment.default)
+    user = User.create!(:login => 'homer', :email => 'homer at example.com', :password => 'beer', :password_confirmation => 'beer', :environment => environment)
     user.activate
-    user2 = User.create!(:login => 'bart', :email => 'bart at example.com', :password => 'fart', :password_confirmation => 'fart', :environment => Environment.default)
+    user2 = User.create!(:login => 'bart', :email => 'bart at example.com', :password => 'fart', :password_confirmation => 'fart', :environment => environment)
     user2.activate
 
     user.notification_settings.activate_notification "new_comment"
@@ -34,5 +47,5 @@ class PushNotificationHelperTest < ActiveSupport::TestCase
 
     assert_equivalent [user], users
   end
-
 end
+



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ab994e1046a3d09af833a557b1d26ed0324bfcf8...dddd8f13ff1868a98f3c72f0fd729751807bc49a

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ab994e1046a3d09af833a557b1d26ed0324bfcf8...dddd8f13ff1868a98f3c72f0fd729751807bc49a
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170505/8381b546/attachment-0001.html>


More information about the Noosfero-dev mailing list