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

Catching error if a user hasn't been subscribed. #2

Closed
sujeet-agrahari opened this issue Sep 5, 2019 · 1 comment
Closed

Catching error if a user hasn't been subscribed. #2

sujeet-agrahari opened this issue Sep 5, 2019 · 1 comment

Comments

@sujeet-agrahari
Copy link

sujeet-agrahari commented Sep 5, 2019

I am using laravel 5.2.
I have nearly 15k users in my db. I used to send notification to each user based on their registered token. 1000 user at a time along with their token and the message It was taking so long.

Now I am trying to create a common topic for now, and I will subscribe every user to this topic.
I created a simple script to subscribe them all. I know I can't subscribe more than 1000 users at a time.
It's obvious there is many reasons that a user has not been subscribed. for example .
The token has expired.

Here is the code to subscribe all the users.

//Start subscribing all user to a common topic
            //collect all users tokens
            $results = User::select( 'regId')
                            ->get()->toArray();
            $user_tokens = array(); 
            foreach($results as $result) {
                array_push($user_tokens, $result['regId']);
            }
            //subscribe all users in batches
            $batch_size = 1000; // 1000 per request

            $iterations = ceil(count($user_tokens) / $batch_size); // determine how many iterations are needed
            
            for ($i = 0; $i < $iterations; $i++) {
                $offset = $i * $batch_size;
                $batch_of_1000_user_ids = array_slice($user_tokens, $offset, $batch_size);
            
                $messaging->subscribeToTopic('test', $batch_of_1000_user_ids);
            }
//End sbuscribing all users to a common topic 

So I want to know.
How can I know if a user is successfully subscribed?
Does this method $messaging->subscribeToTopic('test', $batch_of_1000_user_ids); generate any error if a user hasn't been subscribed.
Also, does this process stops if a user hasn't been subscribed succsessfuly?

for example - suppose first nth user are subscribed, and there is an error on n+1rst user in that case what will happen . Will this method skip that user and continue subscribing rest users.
It will be easier for me to track how many users has not been subscribed.

I found some reference on the docs on Validate messages Section , but I am not sure if this will work in this case.

try {
    $firebase->getMessaging()->validate($message);
} catch (InvalidMessage $e) {
    print_r($e->errors());
}
@jeromegamez
Copy link
Member

If the request goes through (= no exception has been thrown) you can expect the subscription to be successful.

As for the error cases, unfortunately, I cannot be sure at the moment what happens when one of the tokens "in the middle" of a request fails, so I can't tell you with certainty if the rest still is subscribed, or if the whole process fails.

You can check to which topics a token (=App Instance) is subscribed with https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html#working-with-topic-subscriptions

I hope this helps

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