android集成友盟推送实现离线在线推送及桌面图标角标设置

本人亲测有效,有问题欢迎评论,互相学习

1、集成友盟推送—根据友盟文档集成即可实现在线推送

2、集成后想要实现点击跳转到指定页面,那么就要根据后台返回的数据进行跳转

(1)在线推送—在application下实现此代码

UmengNotificationClickHandler notificationClickHandler =new UmengNotificationClickHandler() {

    @Override

    public void launchApp(Context context, UMessage msg) {

        super.launchApp(context, msg);

        Map extra = msg.extra;

        if (extra.get("pushType").equals("1")) {

        Intent intent =new Intent(context, NoticeBulletionDetailActivity.class);

        intent.putExtra("notifyId",extra.get("businessId"));

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);

    }else if (

        extra.get("pushType").equals("2")){

        Intent intent =new Intent(context, PartyBuildNewsDetailActivity.class);

        intent.putExtra("notifyId",extra.get("businessId"));

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      startActivity(intent);

    }

  }

    @Override

    public void openUrl(Context context, UMessage msg) {

          super.openUrl(context, msg);

}

    @Override

    public void openActivity(Context context, UMessage msg) {

          super.openActivity(context, msg);

          Toast.makeText(context, msg.msg_id, Toast.LENGTH_LONG).show();

          Map extra = msg.extra;

        if (extra.get("pushType").equals("1")) {

        Intent intent =new Intent(context, NoticeBulletionDetailActivity.class);

        intent.putExtra("notifyId",extra.get("businessId"));

      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      startActivity(intent);

  }else if (extra.get("pushType").equals("2")){

      Intent intent =new Intent(context, PartyBuildNewsDetailActivity.class);

      intent.putExtra("notifyId",extra.get("businessId"));

      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      startActivity(intent);

  }

}

    @Override

    public void dealWithCustomAction(Context context, UMessage msg) {

          Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();

  }

};

mPushAgent.setNotificationClickHandler(notificationClickHandler);

MiPushRegistar.register(this,"xxxxxxxx","xxxxxxxxx");

HuaWeiRegister.register(this);

(2)离线推送—需要后台指定跳转页,该activity需要继承UmengNotifyClickActivity重写onMessage方法

public class MiPushActivity extends UmengNotifyClickActivity{

    private WebView webView;

    private TemplateTitle mTemplateTitle;

    private String mTitle;

    private String mUrlSign;

    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

          setContentView(R.layout.activity_mi_push);

          webView =findViewById(R.id.web_build_push);

        mTemplateTitle = findViewById(R.id.title_build_push);

    }


          @Override

          public void onMessage(Intent intent) {

        //此方法必须调用,否则无法统计打开数

                super.onMessage(intent);

                mBody = intent.getStringExtra(AgooConstants.MESSAGE_BODY);

        }


          private void displayView(String body) {

        Gson gson =new Gson();

          PushEntity entity = gson.fromJson(mBody,PushEntity.class);

          String id = entity.extra.businessId;

        if ("1".equals(entity.extra.pushType)){

          mTitle ="公告";

          mUrlSign = Constants.WEB_URL+"/a?notifyId="+id;

        }else if ("2".equals(entity.extra.pushType)){

        mTitle ="新闻";

        mUrlSign = Constants.WEB_URL+"/a?id="+id;

    }

        webView.requestFocus();

        mTemplateTitle.setTitleText(mTitle +"详情");

        webView.getSettings().setJavaScriptEnabled(true);

        syncCookie(mUrlSign);

        webView.loadUrl(mUrlSign);

}

}

3、桌面图标的角标只能在代码中设置,友盟无法完成此操作,需要自己在代码中实现下载此jar包,亲测华为小米有效

下载地址:https://pan.baidu.com/s/1YfkPavA_6npt8mI-Szb7QQ

设置角标数字:

    int badgeCount = 5;

    ShortcutBadger.applyCount(mContext, badgeCount);

移除角标数字:

    ShortcutBadger.removeCount(context);

    or:ShortcutBadger.applyCount(mContext, 0);

你可能感兴趣的:(android集成友盟推送实现离线在线推送及桌面图标角标设置)