Skip to content

Commit

Permalink
Touch tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Cousins committed Jan 9, 2021
1 parent 3378d41 commit aeb2366
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ExpoPushNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ public function fetchReceipts(array $tickets): array
*/
public function createPushNotificationRecord($ticket, $token, \Ramsey\Uuid\UuidInterface $batchId, $notification, $notifiable)
{
$pushToken = PushToken::findByReference($token);
$status = $ticket['status'] === 'error' ? 'error' : 'unknown';
if ($status === 'error' && $ticket['details']['error'] === 'DeviceNotRegistered') {
try {
PushToken::findByReference($token)->expire();
$pushToken->expire();
} catch (\Exception $exception) {
Log::error("Unable to find token $token to expire");
}
}
$pushToken->touch();
return \Relative\LaravelExpoPushNotifications\Models\PushNotification::create([
'batch_id' => $batchId,
'notification' => (array)$notification,
Expand All @@ -96,7 +98,7 @@ public function updatePushNotificationReceiptStatuses(array $tickets, Collection
collect($tickets)->map(function ($ticket, $index) use ($pushNotifications, $receipts) {
$pushNotification = $pushNotifications[$index];
if (isset($ticket['id'])) {
$pushNotification->status = $receipts[$index]['status'];
$pushNotification->status = $receipts[$index]['status'] ?? 'unknown';
if ($pushNotification->status === 'error') {
$pushNotification->errors = $receipts[$pushNotification->ticket]['details']['error'];
}
Expand Down
9 changes: 8 additions & 1 deletion src/Models/PushToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class PushToken extends Model

protected $fillable = [
'token',
'meta'
'meta',
'expired_at',
'last_used_at',
];

protected $table = 'expo_push_tokens';
Expand All @@ -53,4 +55,9 @@ public function expire()
{
$this->update(['expired_at' => Carbon::now()]);
}

public function touch()
{
$this->update(['last_used_at' => Carbon::now()]);
}
}

0 comments on commit aeb2366

Please sign in to comment.