Today, I learned the message of the server-side processing receipt of the Android Advanced course push on the web. This course mainly introduces that when the server pushes the message to the client, the client needs to send a receipt to confirm that it received the push message to calculate the complete push process.
The specific implementation method for the server to push a message to the client, will generate a corresponding UUID to identify the message, and the message and UUID stored in the database, the client receives the message, the UUID is removed and sent to the server side, the service side received this UUID, According to the UUID to the database to delete the corresponding message records, the entire push count is complete. Here we first post the core of the sending code
Public voidSendnotifcationtouser (StringApiKey,StringUsernameStringTitleStringMessageStringURI) {Log.Debug"Sendnotifcationtouser () ..."); Random random= NewRandom ();//This ID is the UUID of the client sending receipts StringId= Integer.Tohexstring (random.Nextint ()); IQ Notificationiq=Createnotificationiq (ID, ApiKey, title, message, URI); Clientsession session=SessionManager.GetSession (username);if(Session!= NULL) {if(Session.GetPresence ().IsAvailable ()) {Notificationiq.Setto (Session.GetAddress ()); Session.Deliver (Notificationiq); }Else{savenotification (ApiKey, username, title, message, URI, id); } }//The message needs to be stored in the database, regardless of whether the user exists or not, until the user receives a message to send feedback and then deletetry {User User=Muserservice.Getuserbyusername (username);if(NULL !=User) {savenotification (ApiKey, username, title, message, URI, id); }} catch (Usernotfoundexception e) {//TODO auto-generated catch blockE.Printstacktrace (); } }
You can see that every time you push a message to the client, it will do the inbound operation.
At the same time, the source code also has a business logic, when the server side detects the client from offline to the online state, will go to the database to find if there is a message for the customer, and some will be taken out to send, the code is as follows
List<Notification> List =Mnotificationsevice.Findnotificationbyusername (Session.GetUserName ());if(NULL != List && List.Size ()> 0) {for (Notification Notification:List){StringApiKey=Notification.Getapikey ();StringTitle=Notification.GetTitle ();StringMessage=Notification.GetMessage ();StringUri=Notification.GetURI (); Mnotificationmanager.Sendnotifcationtouser (ApiKey, session.GetUserName (), title, message, URI); Mnotificationsevice.Deletenotification (notification); } }
A bug in this code is that when a message is detected to be sent to the newly-launched client, the sending method Sendnotifcationtouser is called and the original message is deleted from the database, so the operation Will find that the messages in the Sendnotifcationtouser are
Mnotificationsevice.deletenotification (notification), also deleted together (of course, the original storage of the message is also deleted, but this deletion is correct), and just the storage of the message should not be deleted, You must wait for the client to send receipts back and then delete.
Video author Guo Shen The solution to this bug is as follows, first directly post the code
Public voidSendnotifcationtouser (StringApiKey,StringUsernameStringTitleStringMessageStringUri, Boolean shouldsave) {Log.Debug"Sendnotifcationtouser () ..."); Random random= NewRandom ();//This ID is the UUID of the client sending receipts StringId= Integer.Tohexstring (random.Nextint ()); IQ Notificationiq=Createnotificationiq (ID, ApiKey, title, message, URI); Clientsession session=SessionManager.GetSession (username);if(Session!= NULL) {if(Session.GetPresence ().IsAvailable ()) {Notificationiq.Setto (Session.GetAddress ()); Session.Deliver (Notificationiq); }Else{savenotification (ApiKey, username, title, message, URI, id); } }//The message needs to be stored in the database, regardless of whether the user exists or not, until the user receives a message to send feedback and then deletetry {User User=Muserservice.Getuserbyusername (username);if(NULL !=User&&Shouldsave) {savenotification (ApiKey, username, title, message, URI, id); }} catch (Usernotfoundexception e) {//TODO auto-generated catch blockE.Printstacktrace (); } }
The above code adds a field shouldsave to determine if it is in storage, and when the client is detected to be on-line and the database has failed to send the message to be pushed, pass false
if(NULL != List && List.Size ()> 0) {for (Notification Notification:List){StringApiKey=Notification.Getapikey ();StringTitle=Notification.GetTitle ();StringMessage=Notification.GetMessage ();StringUri=Notification.GetURI (); Mnotificationmanager.Sendnotifcationtouser (ApiKey, session.GetUserName (), title, message, Uri,false); Mnotificationsevice.Deletenotification (notification); } }
This change after the test, found that there is no problem, the client from offline to online, the original database has no messages, to meet the needs.
However, in fact, there is a problem, when the client from offline to on-line and the server side from the database detected that there is a message to push, because the last parameter passed to Sendnotifcationtouser is false, there is no storage operation, So the database does not have the data to send the message, after the client receives a message to send a receipt, the server does not have the corresponding data can be deleted, resulting in the appearance seems to achieve the desired effect.
In response to this problem, I made the following changes, for the client from offline to online status and need to push for a successful message, pull the data from the database, push the message directly, do not delete the message, no longer insert a new message, etc., and then delete the client receipt.
PublicvoidSendnotifcationtouser (StringIdStringApiKey,StringUsernameStringTitleStringMessageStringUri, Boolean shouldsave) {Log.debug ("Sendnotifcationtouser () ..."); IQ Notificationiq = Createnotificationiq (ID, ApiKey, title, message, URI); Clientsession session = Sessionmanager.getsession (username);if(Session! =NULL) {if(Session.getpresence (). isavailable ()) {Notificationiq.setto (session.getaddress ()); Session.deliver (Notificationiq); }Else if(Shouldsave) {savenotification (ApiKey, username, title, message, URI, id); } }//The message needs to be stored in the database, regardless of whether the user exists or not, until the user receives a message to send feedback and then delete Try{User user = Muserservice.getuserbyusername (username);if(NULL! = User && Shouldsave) {savenotification (ApiKey, username, title, message, URI, id); } }Catch(Usernotfoundexception e) {//TODO auto-generated catch blockE.printstacktrace (); } }
There are more ID fields, each time the message is sent, the ID message is to generate a new, for the message before sending, there is no need to generate a new ID (that is, UUID), take out the original message ID is OK, find the location of the message changed to the following
List<Notification> List =Mnotificationsevice.Findnotificationbyusername (Session.GetUserName ());if(NULL != List && List.Size ()> 0) {for (Notification Notification:List){StringApiKey=Notification.Getapikey ();StringTitle=Notification.GetTitle ();StringMessage=Notification.GetMessage ();StringUri=Notification.GetURI ();StringId=Notification.Getuuid (); Mnotificationmanager.Sendnotifcationtouser (ID, ApiKey, session.GetUserName (), title, message, Uri,false); }} This can avoid the author Guo Shen bug, in fact, the idea is very simple, is to resend the message when the message is no longer in the library, but to remove the previous message to send, and then receive the client receipts and then delete.
Android Push Advanced Course Learning notes