forked from AnilRh/rpush
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rpush#561 from powerhome/apns-notification-payload…
…-size-limit
- Loading branch information
Showing
18 changed files
with
267 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
lib/rpush/client/active_model/apns/binary_notification_validator.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/rpush/client/active_model/apns/notification_payload_size_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Rpush | ||
module Client | ||
module ActiveModel | ||
module Apns | ||
class NotificationPayloadSizeValidator < ::ActiveModel::Validator | ||
def validate(record) | ||
limit = record.class.max_payload_bytesize | ||
return unless record.payload.bytesize > limit | ||
record.errors[:base] << "APN notification cannot be larger than #{limit} bytes. Try condensing your alert and device attributes." | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/rpush/client/active_record/apns/active_record_serializable_notification.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module Rpush | ||
module Client | ||
module ActiveRecord | ||
module Apns | ||
module ActiveRecordSerializableNotification | ||
def alert=(alert) | ||
if alert.is_a?(Hash) | ||
write_attribute(:alert, multi_json_dump(alert)) | ||
self.alert_is_json = true if has_attribute?(:alert_is_json) | ||
else | ||
write_attribute(:alert, alert) | ||
self.alert_is_json = false if has_attribute?(:alert_is_json) | ||
end | ||
end | ||
|
||
def alert | ||
string_or_json = read_attribute(:alert) | ||
|
||
if has_attribute?(:alert_is_json) | ||
if alert_is_json? | ||
multi_json_load(string_or_json) | ||
else | ||
string_or_json | ||
end | ||
else | ||
begin | ||
multi_json_load(string_or_json) | ||
rescue StandardError | ||
string_or_json | ||
end | ||
end | ||
end | ||
|
||
def sound=(sound) | ||
if sound.is_a?(Hash) | ||
write_attribute(:sound, multi_json_dump(sound)) | ||
self.sound_is_json = true if has_attribute?(:sound_is_json) | ||
else | ||
write_attribute(:sound, sound) | ||
self.sound_is_json = false if has_attribute?(:sound_is_json) | ||
end | ||
end | ||
|
||
def sound | ||
string_or_json = read_attribute(:sound) | ||
|
||
if has_attribute?(:sound_is_json) | ||
if sound_is_json? | ||
multi_json_load(string_or_json) | ||
else | ||
string_or_json | ||
end | ||
else | ||
begin | ||
multi_json_load(string_or_json) | ||
rescue StandardError | ||
string_or_json | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Rpush::Client::ActiveRecord::Apns2::App do | ||
end if active_record? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "unit_spec_helper" | ||
|
||
describe Rpush::Client::ActiveRecord::Apns2::Notification do | ||
subject(:notification) { described_class.new } | ||
|
||
it_behaves_like 'Rpush::Client::Apns::Notification' | ||
it_behaves_like 'Rpush::Client::ActiveRecord::Notification' | ||
|
||
it "should validate the length of the binary conversion of the notification" do | ||
notification = described_class.new | ||
notification.app = Rpush::Apns2::App.create(name: 'test', environment: 'development') | ||
notification.device_token = "a" * 108 | ||
notification.alert = "" | ||
|
||
notification.alert << "a" until notification.payload.bytesize == 4096 | ||
expect(notification.valid?).to be_truthy | ||
expect(notification.errors[:base]).to be_empty | ||
|
||
notification.alert << "a" | ||
expect(notification.valid?).to be_falsey | ||
expect(notification.errors[:base].include?("APN notification cannot be larger than 4096 bytes. Try condensing your alert and device attributes.")).to be_truthy | ||
end | ||
|
||
describe "multi_json usage" do | ||
describe "alert" do | ||
subject(:notification) { described_class.new(alert: { a: 1 }, alert_is_json: true) } | ||
|
||
it "should call MultiJson.load when multi_json version is 1.3.0" do | ||
allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.3.0')) | ||
expect(MultiJson).to receive(:load).with(any_args) | ||
notification.alert | ||
end | ||
|
||
it "should call MultiJson.decode when multi_json version is 1.2.9" do | ||
allow(Gem).to receive(:loaded_specs).and_return('multi_json' => Gem::Specification.new('multi_json', '1.2.9')) | ||
expect(MultiJson).to receive(:decode).with(any_args) | ||
notification.alert | ||
end | ||
end | ||
end | ||
|
||
it "should default the sound to nil" do | ||
expect(notification.sound).to be_nil | ||
end | ||
|
||
it 'does not overwrite the mutable-content flag when setting attributes for the device' do | ||
notification.mutable_content = true | ||
notification.data = { 'hi' => 'mom' } | ||
expect(notification.as_json['aps']['mutable-content']).to eq 1 | ||
expect(notification.as_json['hi']).to eq 'mom' | ||
end | ||
|
||
it 'does not overwrite the content-available flag when setting attributes for the device' do | ||
notification.content_available = true | ||
notification.data = { 'hi' => 'mom' } | ||
expect(notification.as_json['aps']['content-available']).to eq 1 | ||
expect(notification.as_json['hi']).to eq 'mom' | ||
end | ||
|
||
it 'does confuse a JSON looking string as JSON if the alert_is_json attribute is not present' do | ||
allow(notification).to receive_messages(has_attribute?: false) | ||
notification.alert = "{\"one\":2}" | ||
expect(notification.alert).to eq('one' => 2) | ||
end | ||
end if active_record? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Rpush::Client::Redis::Apns2::App do | ||
end if redis? |
Oops, something went wrong.