Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 關於Android端配置極光推送

關於Android端配置極光推送

編輯:關於Android編程

因為業務需要,android客戶端需要加推送,原來采用的百度推送,但是小米手機有時候收不到,後來換成了極光推送,極光的話所有設備都能收到推送,但是在高峰的時候會推遲,博主說的免費版的,收費的沒用過,也不錯評論

博主用的android studio開發的,.so文件放置的位置啥的可能和eclipse有點不同,感覺用android studio開發jni一類的真的好費勁,博主現在老膩歪了,唉,廢話不多說,進入正題:

首先,去極光推送官網注冊一系列流程,不過貌似不用認證,老長時間注冊的了,忘得差不多了,然後左側有添加新應用

\

開始創建應用,注冊的時候必須把包名寫對,可以直接把清單文件中的包名復制就可了,當然,android studio一套代碼開發多個包的話例外

\

這裡有兩個key,第一個的話是android客戶端需要配置的,然後就是後台,後台兩個都需要,注意的是包名一旦定義便不能更改,定義的話極光推送會自己校驗的,給出相應的提示,告訴能不能使用。

再有就是將相應的配置到自己的app中,博主這裡直接下載了相應的demo,進行配置,這裡的話可以先運行demo,測試一下

因為這個demo的包名和自己的一樣,所以只能存在一個demo,好,現在開始配置自己的demo

第一步需要創建兩個包,下面用箭頭標注了

\

這裡吐槽一下,如果用android studio建立的工程更改過名字的話一般配置的話都不會配置成功的,貌似是因為c語言是弱語言的原因,所以說爭取不要配置改過名的工程。

然後是配置lib包

\

再有就是配置清單文件,最重要的是前面那幾行,還有就是service的intent的具體名稱

\

這個千萬不要忘了,博主第一次沒注意就給忘了,收不到推送

再有就是配置服務,看過騰訊的飛鴿,感覺飛鴿的比極光要好點,能讓服務的存活率更高,因為飛鴿的不僅用到了服務還用到了廣播重啟服務,而極光的僅僅用來接收推送消息有時間大家可以了解一下

\

名字和路徑都要對應,再有就是需要初始化的

\

還有就是一個util,剩下的activity可以根據需要自己定義就可以了

\

還有就是xml文件,考進去就可以了

反過來看一下極光的demo

\

剩下的就是需要初始化,就是入口的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 listView;
    private List 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();
        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();
        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 list;

        public MyPagerAdapter(List 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,寫的很好很詳細

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved