Android学习之友盟5.0推送,分享踩坑总结

这几天都没有写博客,今天集成友盟5.0推送,分享,得闲,总结一下友盟5.0推送分享踩得哪些坑!

之前写的友盟踩坑心得是基于友盟4.3版本的,现在友盟5.0相较于4.3,有了较大的改动,呜呼哀哉!学而时习之吧。

友盟5.0的好处:集成推送,分享可以通过导入module的形式,代码简化,维护性高,(一些资源文件就不用自己慢慢去拷贝了)。

至于缺点,就是刚刚改版,会有很多跟4.3不一样的使用方法,所以需要前人去踩坑了!

友盟推送:

1:添加Module

Android studio导入Module自不必多说,把下载的SDK里面的PushSDK当做Module导入自己的项目。

注意点:

如果是android6.0以上的api编译,需要在PushSDK的build.gradle文件的android{}块内添加useLibrary ‘org.apache.http.legacy’,并把compileSdkVersion的版本号改为23。

2:设置测试模式

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

注意:在application的oncrete()方法中添加。

3:配置AppKey & Umeng Message Secret

  <meta-data
            android:name="UMENG_APPKEY"
            android:value="570ccd8267e5s8e026e002343" />
        <meta-data
            android:name="UMENG_MESSAGE_SECRET"
            android:value="b694ba6b27bbw61ed619fb81757ece13d" />
    <receiver
            android:name="com.umeng.message.NotificationProxyBroadcastReceiver"
            android:exported="false" />
        <receiver
            android:name="com.umeng.message.SystemReceiver"
            android:process=":push">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.umeng.message.MessageReceiver"
            android:exported="false"
            android:process=":push">
            <intent-filter>
                <action android:name="org.agoo.android.intent.action.RECEIVE" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.umeng.message.ElectionReceiver"
            android:process=":push">
            <intent-filter>
                <action android:name="org.agoo.android.intent.action.ELECTION_RESULT_V4" />

                <category android:name="umeng" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.umeng.message.RegistrationReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.up72.ftfx.intent.action.COMMAND" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.umeng.message.UmengMessageBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.umeng.message.UmengService"
            android:exported="true"
            android:label="PushService"
            android:process=":push">
            <intent-filter>
                <action android:name="com.up72.ftfx.intent.action.START" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.up72.ftfx.intent.action.COCKROACH" />
            </intent-filter>
            <intent-filter>
                <action android:name="org.agoo.android.intent.action.PING_V4" />

                <category android:name="umeng" />
            </intent-filter>
        </service>
        <service
            android:name="com.umeng.message.UmengIntentService"
            android:process=":push" />
        <service
            android:name="com.umeng.message.UmengMessageIntentReceiverService"
            android:exported="true"
            android:process=":push">
            <intent-filter>
                <action android:name="org.android.agoo.client.MessageReceiverService" />
            </intent-filter>
            <intent-filter>
                <action android:name="org.android.agoo.client.ElectionReceiverService" />
            </intent-filter>
        </service>
        <service
            android:name="com.umeng.message.UmengMessageCallbackHandlerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.umeng.messge.registercallback.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.unregistercallback.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.message.handler.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.autoupdate.handler.action" />
            </intent-filter>
        </service>
        <!-- If you donot want to use the standard notification bar in SDK, you can define IntentService instead to handle message. -->

        <service
            android:name="com.umeng.message.UmengDownloadResourceService"
            android:exported="false" />
        <service
            android:name="com.umeng.message.local.UmengLocalNotificationService"
            android:exported="false" />

注意:配置在清单文件中,确保和友盟后台注册应用信息是一致的。

4:开启推送服务

在应用主Activity中(一般就是MainActivity)添加

PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        if (mPushAgent.isEnabled() || UmengRegistrar.isRegistered(this)) {
            device_token = mPushAgent.getRegistrationId();
            Log.e("device_token", device_token);
        }

获取Device Token,

注意:device_token要在友盟后台添加测试Token才能进行推送测试,否则机器接受不到。

mPushAgent.disable(); 关闭推送服务

PushAgent.getInstance(context).onAppStart(); 开启统计,在所有Activity中的Oncrete()中添加

5:消息的接收

UmengMessageHandler messageHandler = new UmengMessageHandler() {
            /**
             * 参考集成文档的1.6.3
             * http://dev.umeng.com/push/android/integration#1_6_3
             * */
            @Override
            public void dealWithCustomMessage(final Context context, final UMessage msg) {
                new 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);
                        }
                    }
                });
            }
        };
        mPushAgent.setMessageHandler(messageHandler);
        mPushAgent.enable();
    }

注意:在Application的Oncrete()方法中添加此方法

这样就可以接收到消息了。

友盟分享:(只讲述新浪,QQ,QQzone,微信,朋友圈)

1:集成友盟5.0分享sdk

这里写图片描述

友盟提供了两种集成方式

1.解压SDK压缩包,将文件夹中的’main/libs’和’main/res’文件夹复制到你的项目工程根目录下(如使用’ADT 17’以下用户需要手动添加’libs’下的jar文件到工程Path中)

2.解压SDK压缩包,将文件夹中的social_sdk_library_project文件夹导入Eclipse,并在您的工程中添加对此项目的引用即可。

这里为了方便和维护,我选择了第二种方式,Android studio也可以将其作为module导入。

2:清单文件配置

<activity
            android:name="com.umeng.socialize.editorpage.ShareActivity"
            android:excludeFromRecents="true"
            android:theme="@style/Theme.UMDefault" />
        <activity
            android:name="com.up72.ftfx.wxapi.WXEntryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity
            android:name="com.up72.ftfx.WBShareActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
            android:configChanges="keyboardHidden|orientation"
            android:exported="false"
            android:windowSoftInputMode="adjustResize" />

        <service
            android:name="com.sina.weibo.sdk.net.DownloadService"
            android:exported="false" />

        <activity
            android:name="com.tencent.tauth.AuthActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="tencent100424468" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

具体可以看友盟文档,这里只包括了新浪,QQ,QQZone,微信,朋友圈的

这里写图片描述

注意:微信,已经新浪微博的回调Activity路径
注意:AuthActivity的android:scheme,格式为tencent+appID

权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

3:初始化配置

  void initUM() {
        PlatformConfig.setWeixin("wx79afb257f799bb67", "e6ac6f3e1a9a0c318334e1e35b15df28");
        //微信 appid appsecret
        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");
        //新浪微博 appkey appsecret
        PlatformConfig.setQQZone("1105327228", "GYttMYCpxiGOfY5v");
        // QQ和Qzone appid appkey
    }

注意:集成到Application中,进行初始化

4:分享

public void shareContent() {
        image = new UMImage(getActivity(),
                BitmapFactory.decodeResource(getResources(), R.mipmap.icon));
        new ShareAction(getActivity()).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setShareboardclickCallback(shareBoardlistener)
                .open();

    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
//            Toast.makeText(getActivity(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(getActivity(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {

        @Override
        public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
            switch (share_media) {
                case SINA:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withText(
                                    "测试"
                                            + "url")
                            .withMedia(image)
                            .share();
                    break;
                case QQ:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withText("hello umeng")
                            .withTitle("qqshare")
                            .withMedia(image)
                            .withTargetUrl("http://dev.umeng.com")
                            .share();
                    break;
                case QZONE:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withText("hello umeng")
                            .withMedia(image)
                            .withTargetUrl("https://open.weixin.qq.com/cgi-bin/appdetail?t=manage/detail&type=app&lang=zh_CN&token=d3211865a37fad17d540149aff67a8eee8473739&appid=wx79afb257f799bb67")
                            .share();
                    break;
                case WEIXIN:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withMedia(image)
                            .withText("hello umeng")
                            .share();
                    break;
                case WEIXIN_CIRCLE:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withMedia(image)
                            .share();
                    break;
            }

        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(getActivity()).onActivityResult(requestCode, resultCode, data);
    }

这里集成的很全面,具体还是介绍下吧

我这里为了防止分享内容不一致,将其分开写了

分享一致内容:

/**普通分享面板,分享相同一致内容**/
               new ShareAction(this).setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                      .setShareboardclickCallback(shareBoardlistener)
                       .open();

这里友盟对分享代码做了极大的简化,提高了我们的开发效率与集成速度

注意:

微信分享:微信只注意一个问题—–>签名问题

为了防止每次都打线上的包来测试,我们可以将as的debug.keystore的MD5签名获取到,去掉冒号(:),大写变小写,在微信开发平台上将注册的应用包名改为本机debug签名,测试通了,在改为正式签名。

QQ分享:初始化配置没问题就问题不大,主要问题在QQ空间分享时,会出现

这里写图片描述

这是比较坑壁的,不多说,腾讯拦截了分享链接,用友盟测试链接也不会通过的,换上正式链接可能就不会出现这个问题

以QQ空间分享为例:

                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withText("hello umeng")
                            .withMedia(image)
                            .withTargetUrl("https://open.weixin.qq.com/cgi-bin/appdetail?t=manage/detail&type=app&lang=zh_CN&token=d3211865a37fad17d540149aff67a8eee8473739&appid=wx79afb257f799bb67")
                            .share();

这里withTargetUrl不能少,去掉就会分享不成功。

新浪分享:

注意:新浪分享的title是不显示的,URL链接只能加在分享文字后显示,并且需要确保withText()不为空

当同时传递URL参数和图片时,注意确保图片不能超过32K,否则无法分享,不传递URL参数时图片不受32K限制

 new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
                            .withTitle("分享app")
                            .withText("测试" + "url")
                            .withMedia(image)
                            .share();

不要轻信demo啊,可以作为参考,但不一定完全正确。

至于第三方登录,我也讲讲吧

第三方登录:讲白了,就是授权,获取用户信息两部分

授权部分

 private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

至于获取用户信息

已授权的平台,可以获取用户信息(新浪微博可以获取用户好友列表) 实现的方法与授权和解除授权类似:

mShareAPI = UMShareAPI.get(this);

初始化UMShareAPI,然后进行用户信息获取:

mShareAPI.getPlatformInfo(UserinfoActivity.this, platform, umAuthListener);

对于新浪微博好友列表的获取使用的接口是:

mShareAPI.getFriend(UserinfoActivity.this, SHARE_MEDIA.SINA, umGetfriendListener);

注意要重写

onActivityResult()

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mShareAPI.onActivityResult(requestCode, resultCode, data);
} 

算了,copy一点,关于信息字段,可以打断点看看返回内容,也可以参考我在友盟4.3版本所写的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值