在集成友盟推送的时候,小米和华为对后台进程做了诸多限制。若使用一键清理,应用的channel进程被清除,将接收不到推送。为了增加推送的送达率,可选择接入小米托管弹窗功能。通知将由小米系统托管弹出,点击通知栏将跳转到指定的Activity。该Activity需继承自UmengNotifyClickActivity,同时实现父类的onMessage方法,对该方法的intent参数进一步解析即可,该方法异步调用,不阻塞主线程。
这意思就是如果进程被一键销毁了还想发送推送的话就是要打开小米和华为通道,小米通道很简单,按照步骤走就行,没什么问题
- 打开dev.xiaomi.com,点击右上角进入登录页面。进入登陆界面后,输入帐号、密码并点击登陆后进入小米开放平台。
- 正确创建应用后,获取小米push的Appkey和AppSecret。
- 在【友盟+】新版推送后台http://message.umeng.com的【应用管理】【应用信息】里的x-secret处填上小米Push的AppSecret,并保存。
- 小米Push初始化 :
MiPushRegistar.register(final Context context, final String XIAOMI_ID, final String XIAOMI_KEY);
- 使用小米弹窗功能,就把示例代码copy就行了
public class MipushTestActivity extends UmengNotifyClickActivity {private static String TAG = MipushTestActivity.class.getName();@Overrideprotected void onCreate(Bundle bundle) {super.onCreate(bundle);setContentView(R.layout.activity_mipush);}@Overridepublic void onMessage(Intent intent) {super.onMessage(intent); //此方法必须调用,否则无法统计打开数String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);UmLog.i(TAG, body);Message message = Message.obtain();message.obj = body;handler.sendMessage(message);}
}
需要注意的是,要打开通道务必下载聚合版的U-PUSH版本SDK
//http://dev.umeng.com/push/android/sdk-download 在这里下载聚合版
然后如下图操作就行了
然后点击进入,如下图 操作
然后在application里面初始化一下就ok,我这里提供一个Umeng集成的工具类,使用的时候只需要在application里面初始化一下就行,里面的上下文context自行替换
UmengHelper.getInstance().init();
/*** 友盟推送类*/
public class UmengHelper {public static final String TAG = "UmengHelper";public static final String UPDATE_STATUS_ACTION = "com.umeng.message.example.action.UPDATE_STATUS";public static UmengHelper mUmengHelper;public static String dToken = "";private PushAgent mPushAgent;public static UmengHelper getInstance() {if (mUmengHelper == null) {mUmengHelper = new UmengHelper();}return mUmengHelper;}public void init() {mPushAgent = PushAgent.getInstance(ContextUtils.getContext());//注册推送服务,每次调用register方法都会回调该接口mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);mPushAgent.register(new IUmengRegisterCallback() {@Overridepublic void onSuccess(String deviceToken) {//注册成功会返回device tokendToken = deviceToken;Log.d(TAG, "onSuccess: deviceToken----" + deviceToken);ContextUtils.getContext().sendBroadcast(new Intent(UPDATE_STATUS_ACTION));}@Overridepublic void onFailure(String s, String s1) {Log.d(TAG, "onFailure: ---" + s + " s1--" + s1);ContextUtils.getContext().sendBroadcast(new Intent(UPDATE_STATUS_ACTION));}});UMConfigure.init(ContextUtils.getContext(), "5982b9ca677baa1110001322", "${UMENG_CHANNEL_VALUE}", UMConfigure.DEVICE_TYPE_PHONE, "7673a51a7550d077edeb35874d0a65a4");mPushAgent.onAppStart();getAppInfo();
// setUserTag();
// setUserAlias();setMessageHandler();setNotificationClickHandler();}/*** 设置用户别名*/private void setUserAlias() {//设置用户id和device_token的一对多的映射关系:mPushAgent.addAlias("zhangsan@sina.com", "aaaa", new UTrack.ICallBack() {@Overridepublic void onMessage(boolean isSuccess, String message) {}});//设置用户id和device_token的一一映射关系,确保同一个alias只对应一台设备:mPushAgent.setAlias("zhangsan@sina.com", "aaaa",new UTrack.ICallBack() {@Overridepublic void onMessage(boolean isSuccess, String message) {}});}/*** 设置用户标签*/private void setUserTag() {mPushAgent.getTagManager().addTags(new TagManager.TCallBack() {@Overridepublic void onMessage(final boolean isSuccess, final ITagManager.Result result) {//isSuccess表示操作是否成功}}, "movie", "sport");}public void setMessageHandler() {UmengMessageHandler messageHandler = new UmengMessageHandler() {@Overridepublic void dealWithCustomMessage(final Context context, final UMessage msg) {new Handler().post(new Runnable() {@Overridepublic void run() {// 对自定义消息的处理方式,点击或者忽略boolean isClickOrDismissed = true;if (isClickOrDismissed) {//自定义消息的点击统计UTrack.getInstance(ContextUtils.getContext()).trackMsgClick(msg);} else {//自定义消息的忽略统计UTrack.getInstance(ContextUtils.getContext()).trackMsgDismissed(msg);}Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();}});}@Overridepublic void dealWithNotificationMessage(Context context, UMessage uMessage) {super.dealWithNotificationMessage(context, uMessage);}//自定义通知样式@Overridepublic Notification getNotification(Context context, UMessage msg) {Log.d(TAG, "getNotification 111 msg=" + msg + ", msg.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.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);}}};PushAgent.getInstance(ContextUtils.getContext()).setMessageHandler(messageHandler);}/*** 自定义行为的回调处理,参考文档:高级功能-通知的展示及提醒-自定义通知打开动作* UmengNotificationClickHandler是在BroadcastReceiver中被调用,故* 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK*/public void setNotificationClickHandler() {/*** 该Handler是在BroadcastReceiver中被调用,故* 如果需启动Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK* */UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {@Overridepublic void dealWithCustomAction(Context context, UMessage msg) {Map<String, String> params = new HashMap<>();params.put("msg_id", msg.msg_id);Log.d(TAG, "dealWithCustomAction msg=" + msg.toString() + ", msg.custom=" + msg.custom);}@Overridepublic void launchApp(Context context, UMessage msg) {Log.d(TAG, "launchApp msg=" + msg.toString() + ", msg.custom=" + msg.custom);super.launchApp(context, msg);}@Overridepublic void openUrl(Context context, UMessage msg) {super.openUrl(context, msg);Log.d(TAG, "openUrl msg=" + msg.toString() + ", msg.custom=" + msg.custom);super.openUrl(context, msg);}@Overridepublic void openActivity(Context context, UMessage msg) {super.openActivity(context, msg);Log.d(TAG, "openActivity msg=" + msg.toString() + ", msg.custom=" + msg.custom);super.openActivity(context, msg);}};PushAgent.getInstance(ContextUtils.getContext()).setNotificationClickHandler(notificationClickHandler);}private void getAppInfo() {String pkgName = ContextUtils.getContext().getPackageName();String info = String.format("DeviceToken:%s\n" + "SdkVersion:%s\nAppVersionCode:%s\nAppVersionName:%s",PushAgent.getInstance(ContextUtils.getContext()).getRegistrationId(), MsgConstant.SDK_VERSION,UmengMessageDeviceConfig.getAppVersionCode(ContextUtils.getContext()), UmengMessageDeviceConfig.getAppVersionName(ContextUtils.getContext()));Log.d(TAG, "应用包名:" + pkgName + "\n" + info);}
大家只要导入push的module,然后两个jar包就行了
//umeng-common-1.4.0.jar
//utdid4all-1.1.5.3_proguard.jar
但是华为的通道实在是让人无语,找了半天找不到华为的app secret,后来才发现在一个很不起眼的边角(移动应用详情),如图
然后再按照友盟的集成步骤走就好了;
http://dev.umeng.com/sdk_integate/android_sdk/android_push_doc#5
需要注意的点是,在做离线消息推送的时候,有些人可能找不到,按照如下图示操作就行了
其中这个指定页面就是小米和华为弹窗功能那个类的全路径
附上一个比较全面的友盟SDK集成以及过坑总结
有任何疑惑可以下面发言
关于通道相关的代码,补充一下:
public class MipushDialogActivity extends UmengNotifyClickActivity {
private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {String body = (String) msg.obj;Gson gson = new GsonBuilder().create();PushInfo info = gson.fromJson(body, PushInfo.class);String url = info.getExtra().getYourParam();//获取你定义的那个参数if (url != null) {//跳转到过度页Intent intent = new Intent();intent.putExtra(Constants.SHEME_URL, url);intent.setClassName(MipushDialogActivity.this, Constants.MIDDLEACTIVITY_CLASSNAME);startActivity(intent);overridePendingTransition(0, 0);}finish();}
};@Override
protected void onCreate(Bundle bundle) {super.onCreate(bundle);
}@Override
protected void onNewIntent(Intent intent) {super.onNewIntent(intent);
}@Override
public void onMessage(Intent intent) {super.onMessage(intent); //此方法必须调用,否则无法统计打开数String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);Message message = Message.obtain();message.obj = body;handler.sendMessage(message);
}
}