项目自动集成极光推送的JMessage

项目集成

项目使用自动集成,进行一下四步:

  1. 确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。

    buildscript {
        repositories {
            jcenter()
        }
        ......
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
  2. 在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。

    android {
        ......
        defaultConfig {
            applicationId "com.xxx.xxx" //JPush上注册的包名.
            ......
    
            ndk {
                //选择要添加的对应cpu类型的.so库。
                abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
                // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
            }
    
            manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
                JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
            ]
            ......
        }
        ......
    }
    
    dependencies {
        ......
    
        compile 'cn.jiguang.sdk:jmessage:2.9.0'  // 此处以JMessage 2.9.0 版本为例。
        compile 'cn.jiguang.sdk:jcore:2.0.0'  // 此处以JCore 2.0.0 版本为例。
        ......
    }
    
  3. 在项目中创建一个类并继承JCommonService类,如下:

    package com.talk.nonsense.service;
    
    import cn.jpush.android.service.JCommonService;
    
    public class MyService extends JCommonService {
    }
    
  4. 然后在AndroidManifest.xml文件中application标签下,继续集成:

    <!--这部分中的.MyService是第二步创建的类名-->
    <service
                android:name=".service.MyService"
                android:enabled="true"
                android:exported="false"
                android:process=":pushcore">
                <intent-filter>
                    <action android:name="cn.jiguang.user.service.action" />
                </intent-filter>
            </service>
    

使用登录和注册功能进行验证

  1. 新建一个LoginActivity的Activity。
  2. 修改AndroidManifest.xml文件,将软件启动时显示的页面变为Login,修改如下:
    		<activity android:name=".LoginActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".MainActivity" />
    
  3. 编写登录注册布局,在activity_login.xml文件中编写
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".LoginActivity">
    
        <EditText
            android:id="@+id/login_username_edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="150dp"
            android:layout_marginEnd="8dp"
            android:ems="10"
            android:hint="@string/login_username_text"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/login_password_edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="80dp"
            android:layout_marginEnd="8dp"
            android:hint="@string/login_password_text"
            android:inputType="textPassword"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/login_username_edit" />
    
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/login_password_edit">
    
            <Button
                android:id="@+id/login_login_btn"
                android:text="@string/login_login_text"
                android:layout_width="197dp"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="4dp"
                android:onClick="loginLogin"/>
    
            <Button
                android:id="@+id/login_register_btn"
                android:text="@string/login_register_text"
                android:layout_width="197dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="4dp"
                android:onClick="loginRegister"/>
        </RelativeLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  4. 编写LoginActivity类如下:
    package com.talk.nonsense;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import cn.jpush.im.android.api.JMessageClient;
    import cn.jpush.im.api.BasicCallback;
    
    public class LoginActivity extends AppCompatActivity {
    
        private EditText username;
    
        private EditText password;
    
        private LoginActivity loginActivity = this;
    
    //    public static List<Activity> activities = new ArrayList<>();
    
    //    private boolean isLogin = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
    //        activities.add(this);
    
            // IM的SDK初始化
            JMessageClient.init(this);
    
            username = findViewById(R.id.login_username_edit);
            password = findViewById(R.id.login_password_edit);
    
        }
    
        // 登录点击事件
        public void loginLogin(View view) {
    
            if(username == null || password == null) {
                Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.i("Login:", "---------------not null");
    
            String nameStr = username.getText().toString();
            String pwdStr = password.getText().toString();
    
            if (nameStr == null || "".equals(nameStr.trim())
                    || pwdStr == null || "".equals(pwdStr.trim())) {
    
                Toast.makeText(this, "输入错误!", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.i("Login:", "---------------not space");
    
            //用户名以字母或者数字开头。支持字母、数字、下划线、英文点、减号、 @。长度为Byte(4~128)
            //密码长度Byte(4~128)
            if (!nameStr.matches("([0-9a-z]+)([0-9a-z_\\.]|[@\\-])*")
                    || nameStr.getBytes().length <= 4
                    || nameStr.getBytes().length >= 128
                    || pwdStr.getBytes().length <= 4
                    || pwdStr.getBytes().length >= 128) {
    
                Toast.makeText(this, "用户名或密码不符合规范!", Toast.LENGTH_SHORT).show();
                return;
            }
    
            Log.i("Login:", "---------------OK");
    
            JMessageClient.login(nameStr, pwdStr, new BasicCallback() {
                @Override
                public void gotResult(int i, String s) {
                    Log.i("Login:", "------------" + s);
                    if( i == 0) {
                        //isLogin = true;
                        startActivity(new Intent(loginActivity, MyMainActivity.class));
                        Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "登录失败!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            });
        }
    
        // 注册 点击事件
        public void loginRegister(View view) {
    
            if(username == null || password == null) {
                Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
                return;
            }
    
            String nameStr = username.getText().toString();
            String pwdStr = password.getText().toString();
    
            if (nameStr == null || "".equals(nameStr.trim())
                    || pwdStr == null || "".equals(pwdStr.trim())) {
    
                Toast.makeText(this, "输入错误1", Toast.LENGTH_SHORT).show();
                return;
            }
    
            //用户名以字母或者数字开头。支持字母、数字、下划线、英文点、减号、 @。长度为Byte(4~128)
            //密码长度Byte(4~128)
            if (!nameStr.matches("([0-9a-z]+)([0-9a-z_\\.]|[@\\-])*")
                    || nameStr.getBytes().length <= 4
                    || nameStr.getBytes().length >= 128
                    || pwdStr.getBytes().length <= 4
                    || pwdStr.getBytes().length >= 128) {
    
                Toast.makeText(this, "用户名或密码不符合规范!", Toast.LENGTH_SHORT).show();
                return;
            }
    
            // 注册账号
            JMessageClient.register(nameStr, pwdStr, new BasicCallback() {
                @Override
                public void gotResult(int i, String s) {
    
                    Log.i("Register:", "------------" + i);
                    if (i == 0) {
    
                        //isLogin = true;
                        //startActivity(new Intent(loginActivity, MainActivity.class));
                        Toast.makeText(getApplicationContext(), "注册成功!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "注册失败!", Toast.LENGTH_SHORT).show();
                        return;
                    }
    
                }
            });
    
        }
    
    }
    
    
  5. 最后运行程序,先注册,在登录,如果成功,则JMessage集成成功。

JMessage中的登录和注册

要使用JMessage要先调用JMessageClient.init();方法,进行初始化。

注册

调用方法:

JMessageClient.register(nameStr, pwdStr, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {

                Log.i("Register:", "------------" + i);
                if (i == 0) {
					//注册成功,执行该方法体
                    
                } else {
                	//注册失败,执行该方法体
                    
                }

            }
        });

JMessageClient.register(String var0, String var1, BasicCallback var2)中,var0为用户名,username
var1为密码,password
var2用于回调

登录
JMessageClient.login(nameStr, pwdStr, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                
                if( i == 0) {
                //登录成功,执行该方法体
 
                } else {
                    //登录失败,执行该方法体
                }
            }
        });

同样,上述代码中nameStr为用户名(username),pwdStr为密码(password)。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值