公司开发app时用到了,友盟推送,但是友盟的官方文档写的比较模糊;粗略整理一下从集成友盟推送,到打开推送消息的处理过程;如有处理不合适的地方欢迎指正!!!!
友盟推送官方提供的文档react-native https://developer.umeng.com/docs/66632/detail/67587
//onCreate
PushModule.initPushSDK(this);
PushAgent.getInstance(this).onAppStart();
//onCreate
SoLoader.init(this, /* native exopackage */ false);
//初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
RNUMConfigure.init(context, appKey, channel(Umeng), UMConfigure.DEVICE_TYPE_PHONE, secret);
// initUpush();
到此可以收到推送消息,在rn页面中可以调用桥文件里的方法获取DeviceToken等信息
// An highlighted block
private void initUpush() {
PushAgent mPushAgent = PushAgent.getInstance(this);
handler = new Handler(getMainLooper());
//sdk开启通知声音
mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
// sdk关闭通知声音
// mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
// 通知声音由服务端控制
// mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER);
// mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
// mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
UmengMessageHandler messageHandler = new UmengMessageHandler() {
/**
* 自定义消息的回调方法
*/
@Override
public void dealWithCustomMessage(final Context context, final UMessage msg) {
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 对自定义消息的处理方式,点击或者忽略
boolean isClickOrDismissed = true;
if (isClickOrDismissed) {
//自定义消息的点击统计
UTrack.getInstance(getApplicationContext()).trackMsgClick(msg);
} else {
//自定义消息的忽略统计
UTrack.getInstance(getApplicationContext()).trackMsgDismissed(msg);
}
Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
}
});
}
/**
* 自定义通知栏样式的回调方法
*/
@Override
public Notification getNotification(Context context, UMessage msg) {
switch (msg.builder_id) {
case 1:
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.getNotification();
default:
//默认为0,若填写的builder_id并不存在,也使用默认。
return super.getNotification(context, msg);
}
}
};
mPushAgent.setMessageHandler(messageHandler);
/**
* 自定义行为的回调处理,参考文档:高级功能-通知的展示及提醒-自定义通知打开动作
* UmengNotificationClickHandler是在BroadcastReceiver中被调用,故
* 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK
* */
UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
@Override
public void dealWithCustomAction(Context context, final UMessage msg) {
super.dealWithCustomAction(context,msg);
Log.d("umeng---------",msg.extra.toString());
}
};
//使用自定义的NotificationHandler,来结合友盟统计处理消息通知,参考http://bbs.umeng.com/thread-11112-1-1.html
//CustomNotificationHandler notificationClickHandler = new CustomNotificationHandler();
mPushAgent.setNotificationClickHandler(notificationClickHandler);
//注册推送服务 每次调用register都会回调该接口
mPushAgent.register(new IUmengRegisterCallback() {
@Override
public void onSuccess(String deviceToken) {
UmLog.i(TAG, "device token: " + deviceToken);
}
@Override
public void onFailure(String s, String s1) {
UmLog.i(TAG, "register failed: " + s + " " + s1);
}
});
}
欢迎指正!!!