From ed1731fd95b7a13e0fee7d7fbd5efa56cc0e3a04 Mon Sep 17 00:00:00 2001 From: Josh Puetz Date: Mon, 13 Sep 2021 11:15:58 -0500 Subject: [PATCH] Change mobile new comment notification text and add sound (#14678) --- app/models/device.rb | 5 +++-- .../notifications/new_comment/send.rb | 8 +++++-- spec/models/device_spec.rb | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/models/device.rb b/app/models/device.rb index 329282e4c..3373add29 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -38,9 +38,10 @@ class Device < ApplicationRecord alert: { title: Settings::Community.community_name, subtitle: title, - body: body + body: body.truncate(512) }, - "thread-id": Settings::Community.community_name + "thread-id": Settings::Community.community_name, + sound: "default" }, data: payload } diff --git a/app/services/notifications/new_comment/send.rb b/app/services/notifications/new_comment/send.rb index b51335050..e65824b8b 100644 --- a/app/services/notifications/new_comment/send.rb +++ b/app/services/notifications/new_comment/send.rb @@ -3,6 +3,8 @@ module Notifications module NewComment class Send + include ActionView::Helpers::TextHelper + def initialize(comment) @comment = comment end @@ -40,8 +42,10 @@ module Notifications PushNotifications::Send.call( user_ids: targets, - title: "@#{comment.user.username}", - body: "Re: #{comment.parent_or_root_article.title.strip}", + title: "💬 New Comment", + body: "#{comment.user.username} commented on " \ + "#{comment.commentable.title.strip}:\n" \ + "#{strip_tags(comment.processed_html).strip}", payload: { url: URL.url(comment.path), type: "new comment" diff --git a/spec/models/device_spec.rb b/spec/models/device_spec.rb index e73f5835f..dee168940 100644 --- a/spec/models/device_spec.rb +++ b/spec/models/device_spec.rb @@ -2,6 +2,7 @@ require "rails_helper" RSpec.describe Device, type: :model do let(:device) { create(:device) } + let(:user) { create(:user) } describe "validations" do subject { device } @@ -14,4 +15,25 @@ RSpec.describe Device, type: :model do it { is_expected.to validate_uniqueness_of(:token).scoped_to(%i[user_id platform consumer_app_id]) } end end + + describe "#create_notification" do + let(:data_hash) { { "alert" => "Hello World" } } + + context "when iOS device" do + let(:consumer_app_ios) { create(:consumer_app, platform: :ios) } + + it "creates an Apns2 notification" do + mocked_objects = mock_rpush(consumer_app_ios) + + device = create(:device, user: user, platform: "iOS") + device.create_notification("Subtitle", "Body", data_hash) + expect(mocked_objects[:rpush_notification].data[:aps][:alert][:title]).to eq(Settings::Community.community_name) + expect(mocked_objects[:rpush_notification].data[:aps][:alert][:subtitle]).to eq("Subtitle") + expect(mocked_objects[:rpush_notification].data[:aps][:alert][:body]).to eq("Body") + expect(mocked_objects[:rpush_notification].data[:aps][:"thread-id"]).to eq(Settings::Community.community_name) + expect(mocked_objects[:rpush_notification].data[:aps][:sound]).to eq("default") + expect(mocked_objects[:rpush_notification].data[:data]).to eq(data_hash) + end + end + end end