最近项目使用推送是友盟推送,实际上推送原理相同,但是友盟推送和极光推送在Android端的代码编写还是有很大差别的。极光推送我记得是定义广播,然后在广播中发送通知,在设置跳转以一类的,但是在友盟推送中,友盟封装好了,有响应的回调接口。
极光推送踩坑:
http://blog.csdn.net/lmq121210/article/details/77100935
友盟推送自定义通知:
final UmengMessageHandler messageHandler = new UmengMessageHandler() {
//自定义通知
@Override
public Notification getNotification(Context context, UMessage msg) {
switch (msg.builder_id) {
case BUILD_ID:
return super.getNotification(context, msg);
default:
//默认为0,若填写的builder_id并不存在,也使用默认。
Notification.Builder builder = new Notification.Builder(context);
RemoteViews myNotificationView = new RemoteViews(context.getPackageName(),
R.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.build();
}
}
//自定义消息
@Override
public void dealWithCustomMessage(final Context context, final UMessage msg) {
new Handler(context.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// 对于自定义消息,PushSDK默认只统计送达。若开发者需要统计点击和忽略,则需手动调用统计方法。
boolean isClickOrDismissed = true;
if (isClickOrDismissed) {
//自定义消息的点击统计
UTrack.getInstance(context).trackMsgClick(msg);
} else {
//自定义消息的忽略统计
UTrack.getInstance(context).trackMsgDismissed(msg);
}
Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
}
});
}
};
PushAgent.getInstance(context).setMessageHandler(messageHandler);
点击通知跳转的回调,友盟已经帮我们写好了:
UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
@Override
public void launchApp(Context context, UMessage msg) {
super.launchApp(context, msg);
/**
* TODO 点击跳转,后续处理
*/
}
};
PushAgent.getInstance(context).setNotificationClickHandler(notificationClickHandler);
如果需要跳转到相应的activity,就需要和服务器约定了 ,并且在友盟开发者平台配置。