友盟自定义消息

 最近根据项目需要,做了个消息推送,使用友盟消息推送,开始根据友盟的Api的实例进行做,可是到传数据到你需要的Activity中,就是始终都传不过去,最后发现他们都不在一个进程中,但是不用担心,友盟还是很友好的,你只需要继承UmengBaseIntentService服务,在再onMessage()方法中去获取推送过来的消息,在根据你的需求进行操作。

1、在你需要的MainActivity中进行操作。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UmengUpdateAgent.update(this);
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        mPushAgent.onAppStart();
        mPushAgent.setDebugMode(true);

        Runnable addAliasRunnable = new Runnable() {
            public void run() {
                try {
                     //根据业务需要自定义的Alias的类型
                    mPushAgent.addAlias(getUserId(), Constants.ALIAS_TYPE_NEW_MSG);
                    mPushAgent.addAlias(getUserId(), Constants.ALIAS_TYPE_COOP);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(addAliasRunnable).start();

        mPushAgent.setPushIntentServiceClass(PushIntentService.class);
        mPushAgent.getMessageHandler();
        
    }
2. 在你应用的Application的onCreate方法中去获取推送过的消息,注意,现在还没在应用的进程中。

public class OwnerApplication extends Application {

    static Context mContext;
    static Resources mResource;
    private PushAgent mPushAgent;


    public static synchronized OwnerApplication context() {
        return (OwnerApplication) mContext;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        mContext = getApplicationContext();
        mResource = mContext.getResources();
        Fresco.initialize(this);
        Stetho.initialize(Stetho
                .newInitializerBuilder(this)
                .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                .enableWebKitInspector(
                        Stetho.defaultInspectorModulesProvider(this)).build());

        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        mPushAgent.setDebugMode(true);
    }

}

3. 在继承友盟的UmengBaseIntentService服务进行取数据,当然我在是使用的广播进行数据发送,然后你需要的MainActivity中去注册广播进行获取数据。

public class PushIntentService extends UmengBaseIntentService {


    @Override
    protected void onMessage(Context context, Intent intent) {
        super.onMessage(context, intent);
        String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);
        try {
            UMessage msg = new UMessage(new JSONObject(message));
            Intent localIntent=new Intent();
            localIntent.setAction(Constants.ACTION_NEW_MESSAGE);
            localIntent.putExtra(Constants.INTENT_NEW_MESSAGE,msg.custom);//msg.custom就是我们要获取的数据
            sendBroadcast(localIntent);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值