Skip to content
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

User with multiple devices #119

Open
icemancast opened this issue Jul 28, 2020 · 8 comments
Open

User with multiple devices #119

icemancast opened this issue Jul 28, 2020 · 8 comments

Comments

@icemancast
Copy link

Using the notification channel how would i handle if a user has multiple devices? Currently I have a users table and a devices table that's a one to many. The notification setup shows:

/**

  • Route notifications for the Apn channel.
  • @return string|array
    */
    public function routeNotificationForApn()
    {
    return $this->ios_push_token;
    }

Which I assume would go in the users model. But I don't have a token for users I have a devices table that holds a users device. Any one user could have an iphone, ipad or any other device. Currently I pull all devices and send that list for a push which is working but i can't do stuff for instance of counting the users unread messages to badge as the list is all devices in the database. Does that make sense? Should i be iterating over the users and then sending their devices only? that doesn't seem like I should but just wondering.

@ExpDev07
Copy link

ExpDev07 commented Jul 28, 2020

The setup should be pretty easy once we know that the Apn Notification Channel can accept an array of recipients (https://github.com/Edujugon/PushNotification/blob/master/src/Apn.php#L289). Just make sure your Device model has the ios_push_token attribute, and then you can simply define your method as such:

public function routeNotificationForApn()
{
    // Simply return an array of the push tokens (multiple recipients).
    $tokens = $this->devices()->get()->map(function ($device) {
        $device->ios_push_token;
    });
    return $tokens->toArray();
}

@icemancast
Copy link
Author

Okay, yeah it does have a token it's called something different but believe that will still work. This goes in the user model correct?

@ExpDev07
Copy link

ExpDev07 commented Jul 28, 2020

@icemancast Yes. The value that you return is the recipient of the notification, or if you return an array, recipients. It's given that your User model has a devices relationship.

public function devices()
{
    return $this->hasMany(Device::class);
}

@icemancast
Copy link
Author

How would you then get the feedback request if using this so I can unregister devices?

@ExpDev07
Copy link

Just delete the device row from the database?

@icemancast
Copy link
Author

yes the feedback() method on a push gives back the failed tokens so I soft delete them from the database. I guess what I am trying to get to is mass notify however I can't send the badge(1) per user when i mass send the tokens through a push.

I don't think i should iterate through each user and then grab their devices to send the unread badge number to.

@ExpDev07
Copy link

ExpDev07 commented Aug 2, 2020

You can iterate through all users and send a notification, but for this you should use a job service (e.g redis).

@icemancast
Copy link
Author

Okay @ExpDev07 and there should not be a rate limit for apple or android when doing this? I already have notifications working except for this part of the badge. I tried to do through laravel notification channel but didn't work that way and will likely refactor later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants