Android端 配置极光推送

时间:2024-09-18 09:36:38

由于业务须要,androidclient须要加推送。原来採用的百度推送。可是小米手机有时候收不到。后来换成了极光推送,极光的话全部设备都能收到推送,可是在高峰的时候会推迟。博主说的免费版的,收费的没用过,也不错评论

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

首先,去极光推送官网注冊一系列流程,只是貌似不用认证,老长时间注冊的了,忘得差点儿相同了,然后左側有加入新应用

Android端 配置极光推送

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

Android端 配置极光推送

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

再有就是将对应的配置到自己的app中。博主这里直接下载了对应的demo。进行配置,这里的话能够先执行demo。測试一下

由于这个demo的包名和自己的一样,所以仅仅能存在一个demo,好,如今開始配置自己的demo

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

Android端 配置极光推送

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

然后是配置lib包

Android端 配置极光推送

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

Android端 配置极光推送

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

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

Android端 配置极光推送

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

Android端 配置极光推送

还有就是一个util。剩下的activity能够依据须要自定义就能够了

Android端 配置极光推送

还有就是xml文件,考进去就能够了

反过来看一下极光的demo

Android端 配置极光推送

剩下的就是须要初始化,就是入口的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。写的非常好非常具体