How to implement the IM function in the app the second fast realization of the offline message module
An app in the actual use of the chat function, not always stay in the interface, while waiting for friends to send messages, it is possible to jump to other interfaces, it is possible to switch to other apps.
When the current app enters the background and a friend sends a message, you need to push a message to the device. Take Android, for example, to receive an offline message notification requires binding Anpush, the practice is to call Anim.bindanpushservice (AnID, AppKey, Anpushtype).
Call Anim.disconnect () when the app enters the background, and call Anim.connect () when the app returns to the foreground. However, if you write this sentence in every activity to achieve the ability to receive offline functions, it is too complicated.
Here's a tip on how to receive a unified management user's offline messages:
Step One
The establishment of baseactivity, after which all activity is inherited baseactivity.
Step Two
Baseactivity creates a global variable isactive to True, which is used to record whether the current interface is entering the background.
Step Three
When the app enters the background, it writes in the OnStop life cycle method:
@Override protected void onstop () { super.onstop (); // App Access Backstage if (!isapponforeground ()) { try { anim.disconnect (); } catch (arrownockexception e) { e.printstacktrace (); } // global variable isactive = false record is currently in the background isactive = false; } }
Step Four
When the app returns to the foreground, write in the Onresume method:
@Override protected void onresume () { super.onresume (); // app wake-up from the background, enter the front desk if (!isactive) { try { anim.connect (clientId); } catch (arrownockexception e) { e.printstacktrace (); } isActive = true; } }
Step Five
How to determine whether a program is running in the foreground:
In the process to find the current app information, determine whether to run in the foreground private boolean isapponforeground () { activitymanager activitymanager = (ActivityManager) Getapplicationcontext () . Getsystemservice (Context.activity_service); string Packagename =getapplicationcontext (). Getpackagename (); List<runningappprocessinfo>appprocesses = activitymanager.getrunningappprocesses (); if (appprocesses == null) { return false; } for (runningappprocessinfo appprocess : appprocesses) { if (AppProcess.processName.equals (PackageName) && appProcess.importance == RunningAppProcessInfo . Importance_foreground) { return true; } } return false; }
How to implement the IM feature series in the app article:
A common analysis of offline messages
The second fast realization of the offline message module
Three fast implementation of offline message push module
The four create a robust message sending module
The five rapid implementation of picture receiving and sending
The six fast realization of group chat advanced features
Seven fast implementation of chat list sorting module
This article from "Arrow Buckle technology Arrownock" blog, reproduced please contact the author!
How to implement the IM function in the app the second fast realization of the offline message module--Arrow Buckle technology Arrownock