最近在作推送,刚开始接到推送的时候,首先想到了极光推送,后来让用百度推送。程序员都知道,需求不断改,最后变成了信鸽推送。话说对信鸽推送真的是没用过。不过集成第三方的方式都大差不差。还好不负所望,集成了信鸽推送。因为最近很忙,每天1点睡觉。虽然今天一样加班到十点,仍是想记录一下信鸽推送的简单集成。也但愿能对要作信鸽推送的新童鞋有所帮助。(我写文字比较啰嗦,还望原谅。写的细了也防止之后本身忘记了,嘿嘿!:)).
话说官方文档真的是老了,最后看看官方Demo,不过在下认为还有不少是须要上网搜索来帮助的。
废话很少说,看看我集成的结果:android
下面说说集成过程。
首先就是去找到官方文档,没作过的话,就先按照文档一步一步来,虽然文档介绍的并不深刻,不过仍是很通俗易懂的,各类问题后续再解决。百度一下腾讯信鸽进入官网登陆下,找到开发者中心:程序员
如图根据AndroidSDK快速接入一个Demo。后续能够进行完整接入。
我在这里只是简单集成一下,至于不少其余的自定义的,因为明天还要加班,今天就先不写了。网络
接入一个应用。根据提示填写就能够了。ide
接入应用完成后就是这样了。
在应用配置里能够看到ACCESS ID和ACCESS KEY用来集成用的,要在清单文件里填写。
下面就是进行AndroidManifest.xml配置和集成.so文件及jar包了,比较简单,直接复制官方Demo里的就好了。学习
这些SDK里面都有,按照说明放入对应文件夹了便可。spa
<!-- 【必须】 信鸽SDK所需权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.VIBRATE" /><!-- 【可选】 信鸽SDK所需权限 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BATTERY_STATS" />
按照说明配置权限和必要的服务及广播.net
<!-- 【必须】 (2.30及以上版新增)展现通知的activity --> <activity android:name="com.tencent.android.tpush.XGPushActivity" android:theme="@android:style/Theme.Translucent" android:exported="false" > <intent-filter> <!-- 若使用AndroidStudio,请设置android:name="android.intent.action"--> <span style="background-color: rgb(0, 153, 0);"> <action android:name="android.intent.action" /></span> </intent-filter> </activity> <!-- 【必须】 信鸽receiver广播接收 --> <receiver android:name="com.tencent.android.tpush.XGPushReceiver" android:process=":xg_service_v2" > <intent-filter android:priority="0x7fffffff" > <!-- 【必须】 信鸽SDK的内部广播 --> <action android:name="com.tencent.android.tpush.action.SDK" /> <action android:name="com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE" /> <!-- 【必须】 系统广播:网络切换 --> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <!-- 【可选】 系统广播:开屏 --> <action android:name="android.intent.action.USER_PRESENT" /> <!-- 【可选】 一些经常使用的系统广播,加强信鸽service的复活机会,请根据须要选择。固然,你也能够添加APP自定义的一些广播让启动service --> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> <!-- 【可选】 usb相关的系统广播,加强信鸽service的复活机会,请根据须要添加 --> <intent-filter android:priority="0x7fffffff" > <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <action android:name="android.intent.action.MEDIA_CHECKING" /> <action android:name="android.intent.action.MEDIA_EJECT" /> <data android:scheme="file" /> </intent-filter> </receiver> <!-- 【必须】 信鸽service --> <service android:name="com.tencent.android.tpush.service.XGPushService" android:exported="true" android:persistent="true" android:process=":xg_service_v2" /> <!-- 【必须】 通知service,其中android:name部分要改成当前包名 --> <service android:name="com.tencent.android.tpush.rpc.XGRemoteService" android:exported="true" > <intent-filter> <!-- 【必须】 请修改成当前APP名包.PUSH_ACTION,如demo的包名为:com.qq.xgdemo --> <action android:name="</span><span style="font-family:Microsoft YaHei;font-size:18px;color:#3366ff;">com.student.kevin.xingepushdemo</span><span style="font-size: 18px;">.PUSH_ACTION" /> </intent-filter> </service> <!-- 已废弃,可删除之;若不删除也不会有任何反作用 --> <!--<service android:name="com.tencent.android.tpush.service.XGDaemonService" android:process=":qq_push_daemon" /> --> <!-- 【可选】信鸽拉起其余APP应用,若应用在本地找不到能够自动下载 --> <!-- <service android:name="com.tencent.android.tpush.XGDownloadService" android:enabled="true" android:exported="false" /> --> <!-- 【可选】APP实现的Receiver,用于接收消息透传和操做结果的回调,请根据须要添加 --> <!-- YOUR_PACKAGE_PATH.CustomPushReceiver须要改成本身的Receiver: --> <receiver android:name="</span><span style="color:#3366ff;font-size: 18px;">com.student.kevin.xingepushdemo</span><span style="font-size: 18px;">.MessageReceiver" android:exported="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> <!-- 【必须】 请修改成APP的AccessId,“21”开头的10位数字,中间没空格 --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="</span><span style="color:#6633ff;font-size: 18px;">2100216288</span><span style="font-size: 18px;">" /> <!-- 【必须】 请修改成APP的AccessKey,“A”开头的12位字符串,中间没空格 --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="</span><span style="color:#3366ff;font-size: 18px;">AKE58N64A5JH</span><span style="font-size: 18px;">" />
好了,配置完成了,下面就是再代码中注册了,MainActivity的onCreat方法中注册:3d
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); XGPushConfig.enableDebug(getApplicationContext(),true); setContentView(R.layout.activity_main); Context context = getApplicationContext(); // XGPushManager.registerPush(context);//直接注册,没法肯定是否注册成功 //想要知道是否注册成功就添加回调。 XGPushManager.registerPush(context, new XGIOperateCallback() { @Override public void onSuccess(Object data, int flag) { Log.d("TPush", "注册成功,设备token为:" + data); } @Override public void onFail(Object data, int errCode, String msg) { Log.d("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg); } }); }
到此,推送的简单集成已经完成,建立一个推送消息试试吧!code
关于自定义,后面有时间了再继续添加!后面我但愿把全部推送都记录下来。(百度已经写好Demo)
文章写的仓储,还望见谅。能把功能写好就是硬道理。嘿嘿~xml
欢迎共同窗习交流!点击下载信鸽推送Demo!
点击下载百度推送Demo!晚安,祝程序员们之后都少加班不加班,事业有成!:)Good Night!