Android Push Advanced Course Learning notes

Source: Internet
Author: User

Today, I learned the message of server-side processing receipts for Android advanced courses 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 the push message was received before the complete push process.
A detailed implementation method for the server pushes a message to the client, generates a corresponding UUID to identify the message, and stores the message and the UUID in the database. After the client receives the message, the UUID is removed and the UUID is sent to the server side, which is received by the service. According to the UUID to the database to delete the corresponding message records, the entire push calculation is complete.

This is the first place to put out the core of the sending code

 Public voidSendnotifcationtouser (StringApiKey,StringUsernameStringTitleStringMessageStringURI) {Log.Debug"Sendnotifcationtouser () ..."); Random random= NewRandom ();//This ID is the UUID that the client sends the receipt accordingly            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); }        }//Whether the user exists or not, the message needs to be stored in the database 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 detection to the client from offline to online status, will go to the database to find if there is a message for the customer, and some will be taken out to send, code such as 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 (); Mnotificationmanager.Sendnotifcationtouser (ApiKey, session.GetUserName (), title, message, URI); Mnotificationsevice.Deletenotification (notification); }                    }

A bug in this code is when it detects that there is a message to send to the client that is just on-line. Call the Send method Sendnotifcationtouser. and deleted the original message from the database. After this operation, the message that is now sendnotifcationtouser in the library is sent
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 the receipt back before deleting it.

Video author Guo Shen A workaround for this bug such as the following. Paste the code directly first

 Public voidSendnotifcationtouser (StringApiKey,StringUsernameStringTitleStringMessageStringUri, Boolean shouldsave) {Log.Debug"Sendnotifcationtouser () ..."); Random random= NewRandom ();//This ID is the UUID that the client sends the receipt accordingly            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); }        }//Whether the user exists or not, the message needs to be stored in the database 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 infer whether it is in storage, at the same time when the client is detected on the line and the database has failed to send the message to be pushed, passed 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); }                    }

After this change, we found that no matter what the problem, the client from offline to online, the original database has no messages, to meet the needs.

But. In fact there is a problem, when the client from offline to on-line and the server side from the database detection to have the message to push, because the last parameter of the incoming Sendnotifcationtouser is false, there is no inbound operation. So the database does not have the data to send the message, after the client receives the message sends the receipt, the server does not have the corresponding data to be able to delete, causes appears to appear to achieve the expected effect.

In response to this problem. I made changes such as the following, for the client from offline to online status and need to push a successful message before the push, from the database to pull the data, directly push the message, do not delete the message. The new message is no longer inserted, and then deleted when the client receipt is received.

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); }        }//Whether the user exists or not, the message needs to be stored in the database 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 sending a message, and the ID message is generating a new one. For the message before it is sent. It is not necessary to generate a new ID (that is, UUID), the ID of the original message can be removed, find the location of the message to such as 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 will prevent the author from Guo Shen the bug. In fact the idea is very easy. It is the time to send the message again when the message is no longer in the library, but to remove the previous message to send. Wait until you receive a client receipt and then delete it.

Android Push Advanced Course Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.