New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Payload.php #131
Update Payload.php #131
Conversation
Fix for able to do like this: public function toApnVoip($notifiable) { return ApnVoipMessage::create() ->body(json_encode($this->card)) ->custom('aps', ['alert' => ['incoming_caller_id' => $this->card->patient->user_id, 'incoming_caller_name' => $this->card->patient->fullName, 'uuid' => \uniqid()]]) ->badge(1); }
In case when alert used as string (as I understand, it should be JSON), serializator serialize JSON string to another JSON, so alert becomes JSON string in field alert except JSON object Example: Needed - {"aps":{"alert":{"a":1,"b":2}}} Real - {"aps": {"alert":"{\"a\":1,\"b\":2}"}}
src/Payload.php
Outdated
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert; | ||
} elseif(is_string($this->alert)){ | ||
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = json_decode($this->alert, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if $this->alert
is a string, but it is not valid json? Seems we need one more check - if it is a valid json then use json_decode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I will add check.
} | ||
|
||
$this->customValues[$key] = $value; | ||
$this->customValues = array_merge_recursive($this->customValues ? $this->customValues : [], [$key => $value]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we need here the check - if $key === self::PAYLOAD_ROOT_KEY
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because now it's not removes previously defined data in PAYLOAD_ROOT_KEY, it's merges.
In my code I do the following:
public function toApnVoip($notifiable)
{
return ApnVoipMessage::create()
->custom('aps', [
'alert' => [
'incoming_caller_id' => (string)$this->card->card_id,
'incoming_caller_name' => $this->card->therapist->fullName,
'uuid' => (string) \Str::uuid(),
'card' => $this->card
]
])
->badge(1);
}
So I set 'aps' as custom array, but I dont removes preciously setted data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be a separate method. Something like addCustomValue, not setCustomValue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be you can just remove check that field named 'aps' ?
$json = json_decode($this->alert, true); | ||
if($json){ | ||
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $json; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still miss the case when $this->alert
is not a json, but just a string. In this case it will not be added to payload, but I think it should be added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want to add exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still miss the case when
$this->alert
is not a json, but just a string. In this case it will not be added to payload, but I think it should be added
I understand that you want ^)
fixed
Now I understand what you want to achieve :) I think it is better to add an ability to add custom value in the Alert class, and not change the payload custom value method. |
The problem is that I also need to change the laravel notification channels in order for it to work :) |
Ok, I done all changes that we both wants. Added array as value for alert in setAlert, so we doesnt need always use JSON serialize and deserialize for settings. Plz check and merge |
@edamov , can you check? And I already created new pull request to Laravel notification channels |
Thanks :) |
Fix for able to do like this:
public function toApnVoip($notifiable)
{
return ApnVoipMessage::create()
->body(json_encode($this->card))
->custom('aps', ['alert' => ['incoming_caller_id' => $this->card->patient->user_id, 'incoming_caller_name' => $this->card->patient->fullName, 'uuid' => \uniqid()]])
->badge(1);
}