因为业务需要,android客户端需要加推送,原来采用的百度推送,但是小米手机有时候收不到,后来换成了极光推送,极光的话所有设备都能收到推送,但是在高峰的时候会推迟,博主说的免费版的,收费的没用过,也不错评论

博主用的android studio开发的,.so文件放置的位置啥的可能和eclipse有点不同,感觉用android studio开发jni一类的真的好费劲,博主现在老腻歪了,唉,废话不多说,进入正题:

首先,去极光推送官网注册一系列流程,不过貌似不用认证,老长时间注册的了,忘得差不多了,然后左侧有添加新应用

android 推送桌面弹窗 安卓推送设置在哪里_List

开始创建应用,注册的时候必须把包名写对,可以直接把清单文件中的包名复制就可了,当然,android studio一套代码开发多个包的话例外

android 推送桌面弹窗 安卓推送设置在哪里_android_02

这里有两个key,第一个的话是android客户端需要配置的,然后就是后台,后台两个都需要,注意的是包名一旦定义便不能更改,定义的话极光推送会自己校验的,给出相应的提示,告诉能不能使用。

再有就是将相应的配置到自己的app中,博主这里直接下载了相应的demo,进行配置,这里的话可以先运行demo,测试一下

因为这个demo的包名和自己的一样,所以只能存在一个demo,好,现在开始配置自己的demo

第一步需要创建两个包,下面用箭头标注了

android 推送桌面弹窗 安卓推送设置在哪里_List_03

这里吐槽一下,如果用android studio建立的工程更改过名字的话一般配置的话都不会配置成功的,貌似是因为c语言是弱语言的原因,所以说争取不要配置改过名的工程。

然后是配置lib包

android 推送桌面弹窗 安卓推送设置在哪里_android 推送桌面弹窗_04

再有就是配置清单文件,最重要的是前面那几行,还有就是service的intent的具体名称

android 推送桌面弹窗 安卓推送设置在哪里_List_05

这个千万不要忘了,博主第一次没注意就给忘了,收不到推送

再有就是配置服务,看过腾讯的飞鸽,感觉飞鸽的比极光要好点,能让服务的存活率更高,因为飞鸽的不仅用到了服务还用到了广播重启服务,而极光的仅仅用来接收推送消息有时间大家可以了解一下

android 推送桌面弹窗 安卓推送设置在哪里_List_06

名字和路径都要对应,再有就是需要初始化的

android 推送桌面弹窗 安卓推送设置在哪里_android 推送桌面弹窗_07

还有就是一个util,剩下的activity可以根据需要自己定义就可以了

android 推送桌面弹窗 安卓推送设置在哪里_List_08

还有就是xml文件,考进去就可以了

反过来看一下极光的demo

android 推送桌面弹窗 安卓推送设置在哪里_List_09

剩下的就是需要初始化,就是入口的activity,或者fragmentactivity,否则的话后台没有办法统计

package cn.edu.sjzc.student.uiActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;

import java.util.ArrayList;
import java.util.List;

import cn.edu.sjzc.student.R;
import cn.edu.sjzc.student.uiFragment.MainTabActivity;
import cn.edu.sjzc.student.util.JpushUtil;
import cn.jpush.android.api.JPushInterface;

public class GuideActivity extends BaseActivity implements OnTouchListener {

    private ViewPager viewPager;
    private List<View> listView;
    private List<View> listDots;

    private int thePos = 0;
    private int oldPosition;
    private int count = 0;

    private long firstTime = 0;
    private long secondTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        JPushInterface.init(this);
        init();
        registerMessageReceiver();  // used for receive msg
        if (isFirstStart()) {
            setTarget();
            setContentView(R.layout.activity_guide);
            initViewPager();
            initDots();
        } else {
            Intent it = new Intent();
            it.setClass(this, AppStartActivicy.class);
            startActivity(it);
            finish();
        }


    }

    // 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。
    private void init() {
        JPushInterface.init(getApplicationContext());
    }


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


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


    @Override
    protected void onDestroy() {
        unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "cn.edu.sjzc.student.broadcasterceiver.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                String messge = intent.getStringExtra(KEY_MESSAGE);
                String extras = intent.getStringExtra(KEY_EXTRAS);
                StringBuilder showMsg = new StringBuilder();
                showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                if (!JpushUtil.isEmpty(extras)) {
                    showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                }
//                setCostomMsg(showMsg.toString());
            }
        }
    }


    /**
     */
    private boolean isFirstStart() {
        SharedPreferences share = getSharedPreferences("fs", MODE_PRIVATE);
        String target = share.getString("isfs", "0");
        if (target.equals("0")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     */
    private void setTarget() {
        SharedPreferences share = getSharedPreferences("fs", MODE_PRIVATE);
        Editor editor = share.edit();
        editor.putString("isfs", "no");
        editor.commit();
    }

    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        listView = new ArrayList<View>();
        LayoutInflater inflater = getLayoutInflater();
        listView.add(inflater.inflate(R.layout.lay1, null));
        listView.add(inflater.inflate(R.layout.lay2, null));
        listView.add(inflater.inflate(R.layout.lay3, null));
        listView.add(inflater.inflate(R.layout.lay4, null));

        for (int i = 0; i < listView.size(); i++) {
            View view = (View) listView.get(i);
            view.setOnTouchListener(this);
        }

        viewPager.setAdapter(new MyPagerAdapter(listView));
        viewPager.setOnPageChangeListener(new MyPagerChangeListener());

    }

    private void initDots() {
        listDots = new ArrayList<View>();
        listDots.add(findViewById(R.id.dot01));
        listDots.add(findViewById(R.id.dot02));
        listDots.add(findViewById(R.id.dot03));
        listDots.add(findViewById(R.id.dot04));
    }

    public class MyPagerChangeListener implements OnPageChangeListener {

        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        public void onPageSelected(int position) {
            ((View) listDots.get(position))
                    .setBackgroundResource(R.drawable.dot_focused);
            ((View) listDots.get(oldPosition))
                    .setBackgroundResource(R.drawable.dot_normal);
            oldPosition = position;
            thePos = position;
            System.out.println(thePos);
        }

    }

    public class MyPagerAdapter extends PagerAdapter {

        private List<View> list;

        public MyPagerAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public void destroyItem(View view, int index, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) view).removeView(list.get(index));
        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object instantiateItem(View view, int index) {
            ((ViewPager) view).addView(list.get(index), 0);
            return list.get(index);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            // TODO Auto-generated method stub
            return view == (object);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }

    public boolean onTouch(View arg0, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction() && thePos == 3) {
            count++;
            if (count == 1) {
                firstTime = System.currentTimeMillis();
            } else {
                secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 0) {
                    Intent it = new Intent();
                    it.setClass(this, LoginDemoActivity.class);
                    startActivity(it);
                    finish();
                }
                count = 0;
                firstTime = 0;
                secondTime = 0;
            }

        }
        return true;
    }

}



最重要的是要放在相应的生命周期中,如果不是在第一配置的话配送会收到但是后台却不能统计,然后需要进行测试

一个是通知栏,另一个是log打印

收到的推送用户可以自定义显示

package cn.edu.sjzc.student.uiActivity;

import cn.edu.sjzc.student.R;
import cn.edu.sjzc.student.app.UserApplication;
import cn.jpush.android.api.JPushInterface;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.TextView;

public class TestActivity extends BaseActivity {
    private TextView jpush_title, jpush_content;
    private String title, content, number;
    private ImageButton jpush_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
        initData();
    }

    private void initView() {
        TextView tv = new TextView(this);
        tv.setText("用户自定义打开的Activity");
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            content = bundle.getString(JPushInterface.EXTRA_ALERT);
        }
        jpush_back = (ImageButton) findViewById(R.id.jpush_back);
        jpush_back.setOnClickListener(this);
        jpush_title = (TextView) findViewById(R.id.jpush_title);
        jpush_content = (TextView) findViewById(R.id.jpush_content);

    }

    private void initData() {
        SharedPreferences userdata = this.getSharedPreferences(UserApplication.USER_DATA, 0);
        number = userdata.getString(UserApplication.USER_DATA_NUMBER, "");
        jpush_title.setText(title);
        jpush_content.setText(content);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.jpush_back:
                finish();
                break;
        }
    }
}

基本这样就配置成功了,剩下的深入的可以看极光的api,写的很好很详细