【IM】极光简单的聊天测试

配置AndroidManifest.xml

    <permission
     android:name="在极光注册的包名.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />

    <!-- Required 一些系统要求的权限,如访问网络等 -->
    <uses-permission android:name="在极光注册的包名.permission.JPUSH_MESSAGE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application

        android:name="自己的Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.sunset.im.IMActivity" >
        </activity>
        <activity android:name="在极光注册的包名.DengluActivity"></activity>
        <!-- Required SDK 核心功能 -->
        <!-- option since 2.0.5 可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process参数 将JPush相关组件设置为一个独立进程 -->
        <!-- 如:android:process=":remote" -->
        <service
            android:name="cn.jpush.android.service.PushService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTER" />
                <action android:name="cn.jpush.android.intent.REPORT" />
                <action android:name="cn.jpush.android.intent.PushService" />
                <action android:name="cn.jpush.android.intent.PUSH_TIME" />
            </intent-filter>
        </service>

        <!-- since 1.1.4 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->
        <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->
        <service
            android:name="cn.jpush.android.service.DaemonService"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="cn.jpush.android.intent.DaemonService" />

                <category android:name="在极光注册的包名" />
            </intent-filter>
        </service>

        <!-- Required Push SDK核心功能 -->
        <receiver
            android:name="cn.jpush.android.service.PushReceiver"
            android:enabled="true" >
            <intent-filter android:priority="1000" >
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />

                <category android:name="在极光注册的包名" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <!-- Optional -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <!-- Required Push SDK核心功能 -->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="在极光注册的包名" />
            </intent-filter>
        </activity>
        <!-- Required Push SDK核心功能 -->
        <service
            android:name="cn.jpush.android.service.DownloadService"
            android:enabled="true"
            android:exported="false" />
        <!-- Required Push SDK核心功能 -->
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />

        <!-- IM Required IM SDK核心功能 -->
        <receiver
            android:name="cn.jpush.im.android.helpers.IMReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter android:priority="1000" >
                <action android:name="cn.jpush.im.android.action.IM_RESPONSE" />
                <action android:name="cn.jpush.im.android.action.NOTIFICATION_CLICK_PROXY" />

                <category android:name="在极光注册的包名" />
            </intent-filter>
        </receiver>

        <!-- Required. Enable it you can get statistics data with channel -->
        <meta-data
            android:name="JPUSH_CHANNEL"
            android:value="developer-default" />
        <!-- Required. AppKey copied from Portal -->
        <meta-data
            android:name="JPUSH_APPKEY"
            android:value="在极光注册的的应用的APPKEY" />

先初始化Application

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("JMessageDemoApplication", "Application onCreate");

        JMessageClient.init(getApplicationContext());
        //是否打印LOG
        JPushInterface.setDebugMode(true);
    }

注册用户

    public void register() {
        JMessageClient.register("用户名", "用户密码", new BasicCallback() {

            @Override
            public void gotResult(int arg0, String arg1) {
                if (arg0==0) {
                    Toast.makeText(MainActivity.this, "注册成功", 0).show();
                }
            }
        });
    }

获取用户信息

    public void getUserInfo() {
        JMessageClient.getUserInfo(FriendName, new GetUserInfoCallback() {
            @Override
            public void gotResult(int arg0, String arg1, UserInfo arg2) {
                System.out.println("............." + arg0);
                System.out.println("............." + arg1);
                System.out.println(arg2.getUserName() + ">>>>>>>>>>>>>"+ arg2.getNickname());
            }
        });
    }

一定要在使用极光SDK的Activity重配置onResume()和onPause()

@Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

登录

public void login() {
        JMessageClient.login(dl_name.getText().toString(), dl_pwd.getText().toString(), new BasicCallback() {

            @Override
            public void gotResult(int arg0, String arg1) {
                if (arg0==0) {
                    Toast.makeText(DengluActivity.this, "登陆成功", 0).show();
                }
            }
        });
    }

发送消息要在开启一个子线程

//创建一个消息对象
Message m = JMessageClient.createSingleTextMessage("朋友的用户名", "要发送的消息");
//发送消息
JMessageClient.sendMessage(m);

接受消息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im);
        //注册一个接受的广播
    JMessageClient.registerEventReceiver(this);
    }

//接受消息的事件
    public void onEventMainThread(MessageEvent event) {
        Message msg = event.getMessage();
        switch (msg.getContentType()) {
        case text:
            // 处理文字消息
            TextContent textContent = (TextContent) msg.getContent();
            textContent.getText();
            txt_sms.append(textContent.getText());
            break;
        }
    }

一定要在onDestory()中去掉广播

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JMessageClient.unRegisterEventReceiver(this);
    }

自定义设置通知栏的跳转界面,默认的是跳转主界面

    public void onEvent(NotificationClickEvent event) {
        Intent notificationIntent = new Intent(this, IMActivity.class);
        this.startActivity(notificationIntent);// 自定义跳转到指定页面
    }
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页