【极光推送Jpush】Android集成极光推送及其基本使用

Jpush接收到推送通知,保存到数据库,然后列表展示
写的不好,还望见谅,不对地方,多多指教

先给出效果图:

在官网推送内容:
极光官网推送

手机接收到通知
手机通知连接收到通知

点击,跳转到展示页面
点击,跳转到列表展示

1、集成Jpush

(1)创建应用,输入包名,下载demo,给出官方链接http://docs.jiguang.cn/jpush/console/Instructions/

(2)手动集成,导入必要文件,给出官方链接 http://docs.jiguang.cn/jpush/client/Android/android_guide/
倒入两个jar,这里写图片描述

导入so文件,这里写图片描述
复制 res/ 中drawable-hdpi, layout, values文件夹中的资源文件到你的工程中 res/ 对应同名的目录下

(3)配置 AndroidManifest.xml

1)添加权限(注意更改包名)
<permission 
        android:name="您应用的包名.permission.JPUSH_MESSAGE"  
        android:protectionLevel="signature" />
    <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.WRITE_SETTINGS" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
2)复制核心代码(注意更改包名以及appkey)
<!-- Required SDK 核心功能-->
        <!-- 可配置android:process参数将PushService放在其他进程中 -->
        <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.8.0 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 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 SDK核心功能-->
        <activity
            android:name="cn.jpush.android.ui.PushActivity"
            android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.NoTitleBar"
            android:exported="false" >
            <intent-filter>
                <action android:name="cn.jpush.android.ui.PushActivity" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="您应用的包名" />
            </intent-filter>
        </activity>
        <!-- SDK核心功能-->
        <activity
            android:name="cn.jpush.android.ui.PopWinActivity"
            android:configChanges="orientation|keyboardHidden"
            android:exported="false"
            android:theme="@style/MyDialogStyle">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="您应用的包名" />
            </intent-filter>
        </activity>

        <!-- Required SDK核心功能-->
        <service
            android:name="cn.jpush.android.service.DownloadService"
            android:enabled="true"
            android:exported="false" >
        </service>

        <!-- Required SDK核心功能-->
        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />

        <!-- User defined. 用户自定义的广播接收器-->
         <receiver
             android:name="您自己定义的Receiver"
             android:enabled="true">
             <intent-filter>
                 <!--Required 用户注册SDK的intent-->
                 <action android:name="cn.jpush.android.intent.REGISTRATION" /> 
                 <!--Required 用户接收SDK消息的intent--> 
                 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> 
                 <!--Required 用户接收SDK通知栏信息的intent-->
                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> 
                 <!--Required 用户打开自定义通知栏的intent-->
                 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> 
                 <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                 <action android:name="cn.jpush.android.intent.CONNECTION" />
                 <category android:name="您应用的包名" />
             </intent-filter>
         </receiver>

        <!-- Required. For publish channel feature -->
        <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
        <!-- 例如: -->
        <!-- 发到 Google Play 的APK可以设置为 google-play; -->
        <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
        <!-- 目前这个渠道统计功能的报表还未开放。-->
        <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>
        <!-- Required. AppKey copied from Portal -->
        <meta-data android:name="JPUSH_APPKEY" android:value="您应用的Appkey"/> 

(4) 创建你自己的广播接收MyReceiver

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    private List<String> list = new ArrayList<>();
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏,完全要开发者写代码去处理

            String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            // 在这里可以做些统计,或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
//此处,我们取出的是通知的内容。还可以取出别的信息,这个大家可自行处理

//			 在这里可以自己写代码去定义用户点击后的行为
            Intent i = new Intent(context, TestActivity.class); 
            i.putExtra("content", content);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else {
        }
    }
}

注意: i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 是需要设置的
此外,当你接收到推送的通知。点击就跳转Activity,所以最好设置一下Activity的启动方式为singleTask

<activity
            android:name=".TestActivity"
            android:launchMode="singleTask"></activity>

(在TestActivity中接受值的时候,有一点需要注意,稍后会说到)
(5)集成 JPush Android SDK 的混淆

请下载4.x及以上版本的proguard.jar, 并替换你Android Sdk “tools\proguard\lib\proguard.jar”
请在工程的混淆文件中添加以下配置:
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { ; }
-dontwarn cn.jiguang.
*
-keep class cn.jiguang.** { *; }

v2.0.5 ~ v2.1.7 版本有引入 gson 和 protobuf ,增加排除混淆的配置。(2.1.8版本不需配置)
gson && protobuf========
-dontwarn com.google.**
-keep class com.google.gson.** {;}
-keep class com.google.protobuf.** {
;}

(6)注册(最好在application中注册)

JPushInterface.init(this); 

此时,启动程序,已经可以接受服务器或者jpush官网推送的消息了(我们接下来将推送的通知 取出来,存入数据库)

2构建数据库

(1)MySQLiteOpenHelper类

/**
 * @describe:
 * @author: Gao Chunfa.
 * @time: 2017/4/8-11:14.
 */

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String NAME = "mysqlite.db";
    private static final int VERSION = 1;

    public MySQLiteOpenHelper(Context context) {
        super(context, NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "create table if not exists message(_id integer primary key autoincrement,content varchar,time long)";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

(2)SqlDao

/**
 * @describe:增加,查询
 * @author: Gao Chunfa.
 * @time: 2017/4/8-11:22.
 */

public class SqlDao {

    private MySQLiteOpenHelper helper;
    private SQLiteDatabase db;

    public SqlDao(Context context) {
        helper = new MySQLiteOpenHelper(context);
    }

//只做最简单的增加和查询操作,大家可自行操作
    //增加
    public void add(String message) {

        db = helper.getWritableDatabase();
        String sql = "insert into message (content , time) values (?,?)";
        db.execSQL(sql, new String[]{message, System.currentTimeMillis() + ""});
        db.close();
    }

	//查询
    public List<String> find() {
        List<String> list = new ArrayList<>();
        db = helper.getReadableDatabase();
        Cursor cursor = db.query("message", null, null, null, null, null, "time desc", "10");
        while (cursor.moveToNext()) {
            list.add(cursor.getString(cursor.getColumnIndex("content")));
        }
        cursor.close();
        db.close();
        return list;
    }

}

(3)TestActivity(用来展示列表)

public class TestActivity extends AppCompatActivity {

    private ListView listView;
    private SqlDao dao;
    private List<String> list = new ArrayList<>();
    private String content;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        listView = (ListView) findViewById(R.id.listView);
        dao = new SqlDao(this);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        
        content = getIntent().getStringExtra("content");
        if (!content.equals("")){
            dao.add(content);
        }

        list.clear();
        list.addAll(dao.find());
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        content = getIntent().getStringExtra("content");
        if (!content.equals("")){
            dao.add(content);
        }
        list.clear();
        list.addAll(dao.find());
        adapter.notifyDataSetChanged();

    }
}

因为之前说到。我们设置启动方式为singleTask;所以当你第二次点击进入该页面(即该回退栈中已经有
该页面的时候),他不会执行onCreate方法,而是执行onNewIntent()方法,所以。我们在这两
个方法中均作了取值操作,然后刷新adapter;此外,activity的getIntent()方法只是获取activity原来的
intent。我们还需要在onNewIntent()方法中重载一下:setIntent(intent);,要不然,接收到的数据
依然为空。

这就是大体一个流程,很多操作都可以根据实际情况而做处理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值