融雲自定義消息通知欄顯示
第一步 首先要註冊個自定義的廣播,代碼如下:
public classCharNotificationReceiverextendsPushMessageReceiver {
@Override
public booleanonNotificationMessageArrived(Context context,PushNotificationMessage message) {
return false;
}
@Override
public booleanonNotificationMessageClicked(Context context,PushNotificationMessage message) {
return false;
}
}
第二步 要在你的 AndroidManifest.xml文件中註冊你剛剛創建的廣播
<!--融雲廣播接受者-->
<receiver android:name="com.cnsunrun.common.receiver.CharNotificationReceiver"
android:exported="true">
<intent-filter>
<action android:name="io.rong.push.intent.MESSAGE_ARRIVED" />
<action android:name="io.rong.push.intent.MI_MESSAGE_ARRIVED" />
<action android:name="io.rong.push.intent.MESSAGE_CLICKED" />
<action android:name="io.rong.push.intent.MI_MESSAGE_CLICKED" />
</intent-filter>
</receiver>
經過以上的兩步之後你成功了一半,但是你還是收不到消息的通知,這個問題也是困惑我了許久,最後發現我沒有設置融雲接受消息的監聽
public class RongCloudEvent implements RongIMClient.OnReceiveMessageListener, RongIMClient.ConnectionStatusListener, RongIM.OnSendMessageListener {
private static RongCloudEvent mRongCloudInstance;
private final Context mContext;
@Override
public boolean onReceived(Message message, int i) {
return false;
}
/**
* 初始化 RongCloud.
*
* @param context 上下文。
*/
public static void init(Context context) {
if (mRongCloudInstance == null) {
synchronized (RongCloudEvent.class) {
if (mRongCloudInstance == null) {
mRongCloudInstance = new RongCloudEvent(context);
}
}
}
}
/**
* 構造方法。
*
* @param context 上下文。
*/
private RongCloudEvent(Context context) {
mContext = context;
initDefaultListener();
}
/**
* 獲取RongCloud 實例。
*
* @return RongCloud。
*/
public static RongCloudEvent getInstance() {
return mRongCloudInstance;
}
/**
* RongIM.init(this) 後直接可註冊的Listener。
*/
private void initDefaultListener() {
RongIM.getInstance().setOnReceiveMessageListener(this);
RongIM.getInstance().setSendMessageListener(this);
RongIM.setConnectionStatusListener(this);
}
@Override
public void onChanged(ConnectionStatus connectionStatus) {
}
@Override
public Message onSend(Message message) {
return message;
}
@Override
public boolean onSent(Message message, RongIM.SentMessageErrorCode sentMessageErrorCode) {
return false;
}
}
第四步 要在你的application中初始化下RongCloudEvent這個類 ,這樣的話就可以監聽到融雲發過來的消息了
RongIM.init(this);
RongCloudEvent.init(this);