0

I am using Push Sharp Library to send push notifications to android devices. Is there any method to broadcast all news at a single shot? instead of sending each news to each device separately?

            IList<Device> deviceList = new List<Device>();
            IList<PushNotification> newsList = new List<PushNotification>();
            deviceList = //method to fetch Device list
            newsList = //method to fetch newsList

            push.RegisterGcmService(new GcmPushChannelSettings("MY GCM KEY"));
            foreach (Device device in deviceList)
            {
                if (device.DeviceType == "android")
                {
                    foreach (PushNotification news in newsList)
                    {                           
                        push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(device.PushToken).WithJson("{\"NewsTitle\":\"" + news.NewsTitle + "\"}"));
                        Thread.Sleep(5000);
                    }
                }
            }
            Console.WriteLine("Waiting for Queue to Finish...");
            //Stop and wait for the queues to drains
            push.StopAllServices();

One more question related to this: My table storing registration IDs across device IDs is getting duplicate entries when the user re-installs the application. So the user getting same notification twice or thrice. How to resolve this? to keep the device Id as a primary key will do? But i have noticed, at times, the device ID is also getting changed after re-installation. Thanks in advance...

Avn
  • 1
  • 1
  • 3
  • This might help [Processing multiple Notifications with PushSharp for ios and android](http://stackoverflow.com/questions/17536259/processing-multiple-notifications-with-pushsharp-for-ios-and-android) – Enumy Sep 05 '14 at 06:40
  • No Hossam.. I have already went through all the discussions available here related to this topic – Avn Sep 05 '14 at 06:42
  • The best way will be if the notification fails because the device token has expired static void DeviceSubscriptionExpired(object sender, string expiredDeviceSubscriptionId, DateTime timestamp, INotification notification) { Console.WriteLine("Device Subscription Expired: " + sender + " -> " + expiredDeviceSubscriptionId); } - then unregister the device token. – Lyubomir Velchev Nov 20 '14 at 13:11

2 Answers2

0

this may help you

private void PushGCM(List<string> registerationIds)
    {
        var push = new PushBroker();

        push.RegisterGcmService(new GcmPushChannelSettings("applicationID"));

        GcmNotification gcmNotifiction = new GcmNotification();
        gcmNotifiction.RegistrationIds.AddRange(registerationIds);
        gcmNotifiction.WithJson(@"{""alert"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}");

        //Stop and wait for the queues to drains
        push.StopAllServices();

    }
0

1) You may use a windows service and quartz scheduler(triggered on button click) to handle the push notification (If I have understood your question correctly!)

Refer the following for quartz:
http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/index.html

2) Regarding the duplicate entries, I think you should update the existing device with new token (matching with deviceid) without adding new one or make the old one inactive.

And, I don't think the device id changes during every re-installation. Only the device token(registration id) changes.

Ramya
  • 11
  • 3