腾讯信鸽集成

2019-09-19 14:56:18 浏览数 (2)

背景

现在移动推送很多大厂都在做,腾讯信鸽跟个推、极光、友盟比算是比较晚的一个了。 但这并不妨碍我用它。

服务端

这边服务端用的C#,有两种方式集成腾讯信鸽: 1.引用官方编译好的XingeApp.dll 推荐,相当简单方便,基本的Api都有。 ps:老版本的NewtonsoftJson(低于10.0.0)可能会报错。 2.调用Restful Api 不谈,稍微麻烦了些。

Android端

推荐使用Android Studio一键引用。就很方便了。(基于AS 3.0.1) build.gradle

代码语言:javascript复制
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        
        ...

        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
        }
        manifestPlaceholders = [
                XG_ACCESS_ID:"your id",
                XG_ACCESS_KEY : "your key",
        ]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    
    ...

    implementation 'com.tencent.xinge:xinge:3.2.2-release'
    implementation 'com.tencent.wup:wup:1.0.0.E-release'
    implementation 'com.tencent.mid:mid:4.0.6-release'
}

注册清单Manifest.xml

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kuaijiajin.julynovel">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".application.MyApplication"
        android:theme="@style/AppTheme">
        <receiver android:name=".receiver.XGReceiver"
            android:enabled="true">
            <intent-filter>
                <!-- 接收消息透传 -->
                <action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" />
                <!-- 监听注册、反注册、设置/删除标签、通知被点击等处理结果 -->
                <action android:name="com.tencent.android.tpush.action.FEEDBACK" />
            </intent-filter>
        </receiver>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在业务界面或者Application处注册

代码语言:javascript复制
public class MyApplication extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        XGPushManager.enableService(this, true);
        XGPushManager.registerPush(this, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object o, int i) {
                LogUtils.dTag("注册成功,设备token为:", o);
            }

            @Override
            public void onFail(Object o, int errCode, String s) {
                ToastUtils.showLong("注册失败,错误码:"   errCode   ",错误信息:"   s);
            }
        });
    }
}

继承receiver实现点击跳转

代码语言:javascript复制
public class XGReceiver extends XGPushBaseReceiver {

    @Override
    public void onTextMessage(Context context,XGPushTextMessage message){

    }

    @Override
    public void onRegisterResult(Context context,int errorCode,XGPushRegisterResult registerMessage){

    }

    @Override
    public void onUnregisterResult(Context context, int errorCode){

    }

    @Override
    public void onSetTagResult(Context context,int errorCode,String tagName){

    }

    @Override
    public void onDeleteTagResult(Context context, int errorCode,String tagName){

    }

    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult notifiShowedRlt){

    }

    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult message){
        if(ActivityUtils.isActivityExists("com.kuaijiajin.julynovel", MainActivity.class.getName())){
            ActivityUtils.finishToActivity(MainActivity.class, false);
            MainActivity activity = (MainActivity) ActivityUtils.getActivityList().get(0);
            if(activity.mHandler != null){
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("content", message.getContent());
                bundle.putString("title", message.getTitle());
                msg.what = 1;
                msg.setData(bundle);
                activity.mHandler.sendMessage(msg);
            }

        }else{
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        }

    }
}

0 人点赞