二、微信小程序集成融云sdk调试聊天室功能

二、微信小程序集成融云sdk调试聊天室功能,第1张

首先需要注册融云账号,配置生成App Key,并开通小程序服务

官网地址: https://www.rongcloud.cn/

开发文档地址: https://docs.rongcloud.cn/v4/views/im/noui/guide/quick/premise/open.html

可根据官方文档一步一步来完成注册

导入并初始化

用于监听各种事件,如聊天室中的用户新发的消息

传入用户的token

到这里基本就可以实现测试简单的聊天室功能了

通过修改下面对应的 XML 文件中的相关属性,可以实现将头像变为圆形:

rc_item_conversation.xml 会话列表

rc_item_message.xml 会话页面

rc_item_conversation_member.xml 设置页面

app:RCShape=“circle” //圆形

app:RCShape=“square”//方形

android:scaleType="centerCrop"//设置

每个xml文件中有两处需替换

为了接收推送消息,您需要自定义一个继承自 PushMessageReceiver 类的 BroadcastReceiver (必须实现,否则会收不到推送消息),实现其中的 onNotificationMessageArrived,onNotificationMessageClicked 然后把该 receiver 注册到 AndroidManifest.xml 文件中。

自定义的 BroadcastReceiver:

public class DemoNotificationReceiver extends PushMessageReceiver {

@Override

public boolean onNotificationMessageArrived(Context context, PushNotificationMessage message) {

return false

}

@Override

public boolean onNotificationMessageClicked(Context context, PushNotificationMessage message) {

return false

}

}

注册到应用的 AndroidManifest.xml 里面:

<receiver

android:exported="true"

android:name="您自定义的 broadcastReceiver 类名">

<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>

onNotificationMessageArrived 用来接收服务器发来的通知栏消息(消息到达客户端时触发),默认return false,通知消息会以融云 SDK 的默认形式展现。如果需要自定义通知栏的展示,在这里实现自己的通知栏展现代码,同时 return true 即可。

onNotificationMessageClicked 是在用户点击通知栏消息时触发 (注意:如果自定义了通知栏的展现,则不会触发),默认 return false 。如果需要自定义点击通知时的跳转,return true 即可。融云 SDK 默认跳转规则如下

只有一个联系人发来一条或者多条消息时,会通过 intent 隐式启动会话 activity,intent 的 uri 如下:

Intent intent = new Intent()

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder builder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

builder.appendPath("conversation").appendPath(type.getName())

.appendQueryParameter("targetId", targetId)

.appendQueryParameter("title", targetName)

uri = builder.build()

intent.setData(uri)

startActivity(intent)

如果你的 AndroidManifest.xml 里面配置 A activity 拦截了这个 intent ,那在点击这条通知栏消息 时就会启动 activity A 。

<activity

android:name="A"

android:launchMode="singleTop"

android:screenOrientation="portrait"

android:windowSoftInputMode="stateHidden|adjustResize">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data

android:host="你的包名"

android:pathPrefix="/conversation/"

android:scheme="rong" />

</intent-filter>

</activity>

多个联系人发来多条消息时,通过 intent 隐式启动会话列表 activity,intent 的 uri 配置如下:

Intent intent = new Intent()

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder builder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

builder.appendPath("conversationlist")

Uri uri = builder.build()

intent.setData(uri)

startActivity(intent)

如果你的 AndroidManifest.xml 里面配置 B activity 拦截了这个 intent,那在点击这条通知栏消息 时就会启动 activity B。

<activity

android:name="B"

android:launchMode="singleTask"

android:screenOrientation="portrait"

android:windowSoftInputMode="stateHidden|adjustResize">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data

android:host="你的包名"

android:path="/conversationlist"

android:scheme="rong" />

</intent-filter>

</activity>

可以在融云开发者后台广播推送-广播消息-推送中,发起远程推送。

点击推送消息时会触发出如下 action 事件:

Intent intent = new Intent()

intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder uriBuilder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

uriBuilder.appendPath("push_message")

.appendQueryParameter("targetId", targetId)

.appendQueryParameter("pushData", pushData)

.appendQueryParameter("pushId", pushId)

.appendQueryParameter("extra", extra)

startActivity(intent)

如果你的 AndroidManifest.xml 里面配置了 C activity 拦截这个 action, 那么点击时就会跳转到 activity C。

<activity

android:name="C"

android:launchMode="singleTask"

android:screenOrientation="portrait">

<intent-filter>

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data

android:host="你的包名"

android:pathPrefix="/push_message"

android:scheme="rong" />

</intent-filter>

</activity>

通过以上步骤,您已完成了融云推送服务的集成。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11957318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存