集成步骤
1、导入依赖
implementation 'com.umeng.sdk:common:1.5.3'
implementation 'com.umeng.sdk:push:4.2.0'
// 添加友盟分享库
implementation 'cn.dlc.android:umeng-share:1.0.4'
2、在Application初始化
/**
* 初始化友盟SDK
*/
private void initUmengSDK() {
/**
* 方法2,直接初始化,app key和channel为null的话,等价于方法1
* 初始化common库
* 参数1:上下文,不能为空
* 参数2:友盟 app key
* 参数3:友盟 channel
* 参数4:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
* 参数5:Push推送业务的secret,需要集成Push功能时必须传入Push的secret,否则传空
*/
UMConfigure.init(this, Information.UmengAppkey, Information.UmengChannel,
UMConfigure.DEVICE_TYPE_PHONE, Information.UmengPushSecret);
PushAgent instance = PushAgent.getInstance(this);
instance.register(new IUmengRegisterCallback() {
@Override
public void onSuccess(String s) {
Log.e("xsw", "推送id友盟token: "+s );
UserHelper.get().saveUmengToken(s);
}
@Override
public void onFailure(String s, String s1) {
Log.e("xsw", s+"---"+s1 );
}
});
UmengNotificationClickHandler umengNotificationClickHandler = new UmengNotificationClickHandler(){
@Override
public void launchApp(Context context, UMessage uMessage) {
Intent mIntent = new Intent(sInstance, WelcomeActivity.class);//自定义通知栏点击事件
mIntent.putExtra("notification",true);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(mIntent);
}
};
instance.setNotificationClickHandler(umengNotificationClickHandler);
UmengMessageHandler messageHandler = new UmengMessageHandler() {
@Override
public Notification getNotification(Context context, UMessage msg) {
LgqLog.e(msg.text+"....推送id==== "+msg.builder_id);//推送内容和ID
Message msgg = new Message();
msgg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("text1",msg.text); //往Bundle中存放数据
msgg.setData(bundle);//mes利用Bundle传递数据
// mHandler.sendMessage(msgg);//用activity中的handler发送消息
mHandler.sendMessageAtTime(msgg,2000);
switch (msg.builder_id) {
case 1://自定义通知栏
Notification.Builder builder = new Notification.Builder(context);
RemoteViews myNotificationView = new RemoteViews(context.getPackageName(),
R.layout.notification_view);
SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式"yyyy年-MM月dd日-HH时mm分ss秒"
String dfd = df.format(new Date());
myNotificationView.setTextViewText(R.id.notification_title, msg.title);
myNotificationView.setTextViewText(R.id.notification_text, msg.text);
myNotificationView.setTextViewText(R.id.timete, dfd);
// 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);
}
}
};
instance.setMessageHandler(messageHandler);
instance.setDisplayNotificationNumber(0);
}