码迷,mamicode.com
首页 > 移动开发 > 详细

Android端 配置极光推送

时间:2016-04-05 17:46:03      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

因为业务需要,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,否则的话后台没有办法统计

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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;
    }
 
}</view></view></view></view></view></view>

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

 

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

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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,写的很好很详细

Android端 配置极光推送

标签:

原文地址:http://blog.csdn.net/amor5216/article/details/51062866

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!