firebase / firebase-admin-dotnet

Firebase Admin .NET SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
356 stars 128 forks source link

duplicate messages when using send batch of messages #372

Open dongphh opened 6 months ago

dongphh commented 6 months ago

[READ] Step 1: Are you in the right place?

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

When I send a list of messages using sendEachAsync method with data read all info from my DB (about 10000 users) and loop each 500 record and send batch then received duplicate messages.

I using code follow in doc: https://firebase.google.com/docs/cloud-messaging/send-message

Relevant Code:

if (FirebaseApp.DefaultInstance == null) { FirebaseApp.Create(new AppOptions() { Credential = GoogleCredential.FromFile(Server.MapPath(ConfigurationManager.AppSettings["FCMKey"])) }); }

var messages = new List() int cnt = 0; foreach (DataRow item in iResult.CursorData.Rows) { cnt++;

                            messages.Add(new Message()
                            {
                                Notification = new Notification()
                                {
                                    Title = item["title"].ToString(),
                                    Body = item["DESCRIPTION"].ToString()
                                },
                                Token = item["GCMID"].ToString()
                            });

                            if (cnt == 500 || cnt==iResult.CursorData.Rows.Count)
                            {
                                try
                                {

                                    string Obj = fcm.SendMultiNotiV2(messages);
                                    messages.Clear();

                                }
                                catch (Exception ex)
                                {
                                    messages.Clear();                                        
                                }
                                finally
                                {
                                    cnt = 0;
                                    messages.Clear();

                                }
                            }
                        }