Android友盟推送接入踩坑记录

SDK初始化

务必在工程的自定义Application类的onCreate()方法中做SDK代码初始化工作

1
2
3
//友盟推送初始化
//UMConfigure.init(this, "Appkey", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "Umeng Message Secret");
UMConfigure.init(context, "******", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "******");

推送消息接收

自定义消息处理Handler

1
mPushAgent.setMessageHandler(MyUmengMessageHandler.getInstance(context));
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
public class MyUmengMessageHandler extends UmengMessageHandler {
private static final String TAG = "yezhou";
private static MyUmengMessageHandler umengMessageHandler;
private Context context;
private Handler handler;

private MyUmengMessageHandler(Context context) {
this.context = context;
handler = new Handler(context.getMainLooper());
}

public static MyUmengMessageHandler getInstance(Context context) {
synchronized (MyUmengMessageHandler.class) {
if (umengMessageHandler == null) {
synchronized (MyUmengMessageHandler.class) {
umengMessageHandler = new MyUmengMessageHandler(context);
}
}
}
return umengMessageHandler;
}

/**
* 自定义消息的回调方法
*/
@Override
public void dealWithCustomMessage(final Context context, final UMessage msg) {
NLog.i(TAG, "dealWithCustomMessage: " + msg.message_id);
handler.post(new Runnable() {
@Override
public void run() {
// 对自定义消息的处理方式,点击或者忽略
boolean isClickOrDismissed = true;
if (isClickOrDismissed) {
//自定义消息的点击统计
UTrack.getInstance(context).trackMsgClick(msg);
NLog.i(TAG, "UmengMessageHandler");
} else {
//自定义消息的忽略统计
UTrack.getInstance(context).trackMsgDismissed(msg);
}
Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
}
});
}

/**
* 自定义通知栏样式的回调方法
*/
@Override
public Notification getNotification(Context context, UMessage msg) {
NLog.i(TAG, "builder_id: " + msg.builder_id);
switch (msg.builder_id) {
case 1:
Notification.Builder builder = new Notification.Builder(context);
RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.layout_notification_view);
myNotificationView.setTextViewText(R.id.notification_title, msg.title);
myNotificationView.setTextViewText(R.id.notification_text, msg.text);
myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg));
myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg));
builder.setContent(myNotificationView)
.setSmallIcon(getSmallIconId(context, msg))
.setTicker(msg.ticker)
.setAutoCancel(true);

return builder.getNotification();
default:
//默认为0,若填写的builder_id并不存在,也使用默认。
return super.getNotification(context, msg);
}
}
}

自定义消息接收服务

1
mPushAgent.setPushIntentServiceClass(PushService.class);
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
public class PushService extends UmengMessageService {

@Override
public void onCreate() {
super.onCreate();
NLog.i(Constants.TAG, "PushService.onCreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NLog.i(Constants.TAG, "PushService.onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onMessage(Context context, Intent intent) {
String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
NLog.i(Constants.TAG, "PushService.onMessage: " + body); //消息体
if (TextUtils.isEmpty(body))
return;
UMessage msg;
try {
msg = new UMessage(new JSONObject(body));
UTrack.getInstance(this).trackMsgClick(msg);
Map<String, String> extra = msg.extra;
NLog.d(Constants.TAG, "custom: " + msg.custom); //自定义消息的内容
NLog.d(Constants.TAG, "title: " + msg.title); //通知标题
NLog.d(Constants.TAG, "text: " + msg.text); //通知内容
if (extra != null) {
//
}
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onDestroy() {
super.onDestroy();
NLog.i(Constants.TAG, "PushService.onDestroy");
}
}

自定义行为的回调处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 自定义行为的回调处理,参考文档:高级功能-通知的展示及提醒-自定义通知打开动作
* UmengNotificationClickHandler是在BroadcastReceiver中被调用,故
* 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK
* */
UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
@Override
public void dealWithCustomAction(Context context, UMessage msg) {
//Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
NLog.i(Constants.TAG, "UmengNotificationClickHandler: " + msg.message_id + msg.extra.get("cpf"));

//Intent intent = new Intent(MainApplication.this, MainActivity.class);
//intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Map<String, String> extra = msg.extra;

}
};
//使用自定义的NotificationHandler,来结合友盟统计处理消息通知
//CustomNotificationHandler notificationClickHandler = new CustomNotificationHandler();
mPushAgent.setNotificationClickHandler(notificationClickHandler);

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :