kaylavanhaverbeck / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

Please help with code to send push to mutliple phones #35

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi...I am able to send Push notifications using the following code template
Please suggest if there is better way to do this batch push using your APIs

//create payload

//open connection with apple server
pushManager.initializeConnection(.....);

for each iphone in database
           pushManager.addDevice("iPhone", device.getDeviceToken());
           client=pushManager.getDevice("iPhone");  
           pushManager.sendNotification(client, payLoad);
           pushManager.removeDevice("iPhone");
end for

//close connection in finally block

Original issue reported on code.google.com by pilotj...@gmail.com on 9 Dec 2010 at 3:28

GoogleCodeExporter commented 8 years ago
instead of looping thru each iphone id and naming the device 'iPhone'...

loop thru each iphone id and add it to the pushmanager as a unique device.

Then send all the notifications in one shot.

like:

for device in database
           pushManager.addDevice("iPhone", device.getDeviceToken());
           client=pushManager.getDevice( device.name ); 
end for

pushManager.sendNotification(client, payLoad);

for device in database
           pushManager.removeDevice(device.name);
end for

Original comment by idbill.p...@gmail.com on 9 Dec 2010 at 5:41

GoogleCodeExporter commented 8 years ago
Hi, 
Thanks for your reply. By looking at the above code skeleton, wouldn't client 
represent the last device in the for loop. Please advise.

Original comment by pilotj...@gmail.com on 9 Dec 2010 at 6:50

GoogleCodeExporter commented 8 years ago
I'm assuming device is a row coming back from your database call.

And device.name is unique to that device 
and device.token is the unique token for said device row.

Original comment by idbill.p...@gmail.com on 9 Dec 2010 at 7:03

GoogleCodeExporter commented 8 years ago
Yes, what you have assumed is correct. I think you did not understand my 
question...

In the below line
pushManager.sendNotification(client, payLoad);

doesn't client represent the last device present in the DB  and so won't the 
notification be sent to only that device.

Please let me know...

Original comment by pilotj...@gmail.com on 9 Dec 2010 at 7:10

GoogleCodeExporter commented 8 years ago
I'm very busy at the moment, so please read the wiki, there are examples 
already provided.

The important part, is to only initialize the connection once, send all the 
notifications, then close the connection.

Bill

Original comment by idbill.p...@gmail.com on 9 Dec 2010 at 7:30

GoogleCodeExporter commented 8 years ago
Okay..Thanks. I did change the code as per your code skeleton and as expected 
only the last device returned from the DB got notified. 

I think it is because you have put
 pushManager.sendNotification(client, payLoad);

outside the for loop. I couldn't locate any example on the wiki for sending 
bulk pushes. Please re-examine you suggested code and reply back when you get 
time

Thanks
John

Original comment by pilotj...@gmail.com on 9 Dec 2010 at 8:19

GoogleCodeExporter commented 8 years ago
I did not understand the line made in Comment #1
"Then send all the notifications in one shot."

Since you are calling 
pushManager.sendNotification(client, payLoad);

outside the loop. So won't only the device represented in 'client' be notified. 
Please advice

Original comment by pilotj...@gmail.com on 10 Dec 2010 at 2:11

GoogleCodeExporter commented 8 years ago
See:
http://code.google.com/p/javapns/wiki/MultipleClientsExample

I have not tested the code, but it should have everything in the right place.

Someone is working on a database connection piece that may streamline this 
process, but I do not know the status at this time.

Original comment by idbill.p...@gmail.com on 11 Dec 2010 at 7:08

GoogleCodeExporter commented 8 years ago

Original comment by idbill.p...@gmail.com on 11 Dec 2010 at 7:08

GoogleCodeExporter commented 8 years ago
Thanks. I have implemented your template into my code.

Original comment by pilotj...@gmail.com on 13 Dec 2010 at 3:29

GoogleCodeExporter commented 8 years ago
Comment by MrPatol, Dec 15 (2 days ago)
missing line before

pushManager.sendNotification( client, aPayload );
should have:

Device client = pushManager.getDevice(user.getUsername());

Original comment by idbill.p...@gmail.com on 17 Dec 2010 at 6:00

GoogleCodeExporter commented 8 years ago
example has been updated

Original comment by idbill.p...@gmail.com on 17 Dec 2010 at 6:01

GoogleCodeExporter commented 8 years ago
Closing issue since example for javapns 1.x has been updated, and because 
javapns 2.0 provides direct support for this.

Original comment by sype...@gmail.com on 7 Sep 2011 at 1:13