应用增添推送方法

应用添加推送方法

应用添加推送方法

本应用以爱就是幸福网的“云中笔友”为例

1、去信鸽网(http://xinge.qq.com/)注册应用得到appId,accessKey,secretKey
2、下载信鸽安卓客户端并导入到app
    2-1:配置androidManifest.xml
    2-2:增加如下代码(供参考,注意新增权限)

 <!-- 腾讯信鸽推送消息 开始-->
        <!-- push服务广播 -->
        <receiver
            android:name="com.tencent.android.tpush.XGPushReceiver"
            android:process=":xg_service_v2" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="com.tencent.android.tpush.action.SDK" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE" />
            </intent-filter>
        </receiver>

        <!-- push服务 -->
        <service
            android:name="com.tencent.android.tpush.service.XGPushService"
            android:exported="true"
            android:process=":xg_service_v2" />
        <!-- 请修改为APP的AccessId -->

        <!-- APP实现的Receiver,用于接收消息和结果反馈 -->
        <!-- com.tencent.android.tpushdemo.CustomPushReceiver需要改为自己的Receiver -->
        <receiver android:name="com.ai9475.biyou.receiver.CustomPushReceiver" >
            <intent-filter>
                <!-- 接收消息透传 -->
                <action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" />
                <!-- 监听注册、反注册、设置/删除标签、通知被点击等处理结果 -->
                <action android:name="com.tencent.android.tpush.action.FEEDBACK" />
            </intent-filter>
        </receiver>

        <!-- 请修改为APP的AccessId -->
        <meta-data
            android:name="XG_V2_ACCESS_ID"
            android:value="xxxxxxxxxxx" />
        <!-- 请修改为APP的AccessKey -->
        <meta-data
            android:name="XG_V2_ACCESS_KEY"
            android:value="xxxxxxxxx" />
        <!-- 腾讯信鸽推送消息 结束-->


<!-- 腾讯信鸽所需 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />

 

 

3、在安卓端里启动服务,并上传accessToken
   3-1:启动代码如下
   3-2:更新用户accessToken到服务器(以便在服务器上向指定用户发消息)

在oncreate里

//信鸽服务模式,发布时一定要为false
        XGPushConfig.enableDebug(this, false);

        // 注册应用(必须调用本接口,否则APP将无法接收到通知和消息)
        // registerPush有2个版本的接口:带账号绑定和不带
        // registerPush可以在APP启动时或用户登陆后调用
        XGPushManager.registerPush(getApplicationContext());

        // XGPushConfig的set接口必须要在startPushService或register之前调用才会及时生效
        // XGPushConfig.getToken()只有在注册成功后才有效
        accessToken = XGPushConfig.getToken(this)

 


4、在服务器上向用户发送通知,比如A评论了B的文章,此时查出B的accessToken,再向B发送通知

5、app打包,我的android studio是 0.4.6,gradle是 com.android.tools.build:gradle:0.7.+
   在 云中笔友module的build.gradle中加入以下代码:

 

//打包so包----------start
task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    extension 'jar'
    from(new File(project(':biyou').getProjectDir(), 'libs')) { include '**/*.so' }
    into 'lib/'
}

tasks.withType(Compile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
//打包so包----------end