在Android集成腾讯云通信的时候遇到一个问题。在Activity中实现了腾讯云通信SDK里的TIMMessageListener类后通过onNewMessage方法接收云端的新消息。
但是,这个监听方法并不能在每个Activity里面实现。
1Activity里面实现了这个方法,并且在测试中接收到了新消息,但是跳转到2Activity之后再次实现了这个方法,并且在测试中也接收到了新消息,再返回到1Activity后就无法监听到新消息了,目测是因为在2Activity中这个方法被重新实现,所以之前实现的监听就报废了,当2Activity被销毁后,1Activity里面的监听无法恢复。
这是我碰到的问题。
我想问一下各位大神,该如何设计这个即时通讯功能。
比如说我需要在每个页面都监听新消息以便发送Notification,另外,需要给部分页面的图标增加角标,顺便在会话页面实现聊天过程。
本人刚刚入门,不是很了解整个流程,请大神指教。
怪我咯2017-04-18 09:18:06
General logic of IM:
Implement an Service
, 用于接收推送消息(即注册SDK的TIMMessageListener
), 然后调用Context.sendOrderedBroadcast(Intent, String)
orderly broadcast message
Implement a android:priority="-999"
的BroadcastReceiver
, 并在AndroidManifest.xml中注册, 用于接收Service
发送出来的有序广播, 并通过Notification
send to the status bar. Since the level is very low, therefore, it will finally receive the above broadcast
InActivity
/Fragment
里注册BroadcastReceiver
用于接收上述有序广播, 由于这个BroadcastReceiver
的默认级别要比2中注册的BroadcastReceiver
的要高, 因此会优先接收到广播, 在这里我们就可以做拦截处理(代码如下): 会话页面打开后, 不再通过状态栏通知收到新消息. Activity
/Fragment
关闭时, 反注册上述BroadcastReceiver
, at this time, the new message reminder notification in the status bar has been restored.
private BroadcastReceiver mReceiverInActivityOrFragment = new BroadcastReceiver() {
// TODO ...
setResultCode(Activity.RESULT_CANCELED);
}
Key knowledge points:
Orderly broadcast
Private permissions
Reference books:
The Definitive Guide to Android Programming 2nd Edition Chapter 27-Broadcast Intent (The first edition is Chapter 30, the English version is as follows)
Android Programming: The Big Nerd Ranch Guide
p.s. The respondent is not a book supporter. The respondent has read this book completely. The knowledge points in the book basically correspond to the official documents. It is equivalent to a Chinese book with official documents, and it is more relevant to the question. Question, so I recommend that the English electronic file can be downloaded from the Internet.