setp3.配置Androidmanifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.dc.xgdemo"><!-- 【必須】 信鴿SDK所需權(quán)限 --><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.WRITE_SETTINGS" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.VIBRATE" /><!-- 【常用】 信鴿SDK所需權(quán)限 --><uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 【可選】 信鴿SDK所需權(quán)限 --><uses-permission android:name="android.permission.RESTART_PACKAGES" /><uses-permission android:name="android.permission.BROADCAST_STICKY" /><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.BLUETOOTH" /><uses-permission android:name="android.permission.BATTERY_STATS" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/APPTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 【必須】 (2.30及以上版新增)展示通知的activity --><activityandroid:name="com.tencent.android.tpush.XGPushActivity"android:exported="false"android:theme="@android:style/Theme.Translucent"><intent-filter><!-- 若使用AndroidStudio,請(qǐng)?jiān)O(shè)置android:name="android.intent.action" --><action android:name="android.intent.action" /></intent-filter></activity><!-- 【必須】 信鴿receiver廣播接收 --><receiverandroid:name="com.tencent.android.tpush.XGPushReceiver"android:process=":xg_service_v2"><intent-filter android:priority="0x7fffffff"><!-- 【必須】 信鴿SDK的內(nèi)部廣播 --><action android:name="com.tencent.android.tpush.action.SDK" /><action android:name="com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE" /><!-- 【必須】 系統(tǒng)廣播:網(wǎng)絡(luò)切換 --><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><!-- 【可選】 系統(tǒng)廣播:開(kāi)屏 --><action android:name="android.intent.action.USER_PRESENT" /><!-- 【可選】 一些常用的系統(tǒng)廣播,增強(qiáng)信鴿service的復(fù)活機(jī)會(huì),請(qǐng)根據(jù)需要選擇。當(dāng)然,你也可以添加APP自定義的一些廣播讓啟動(dòng)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相關(guān)的系統(tǒng)廣播,增強(qiáng)信鴿service的復(fù)活機(jī)會(huì),請(qǐng)根據(jù)需要添加 --><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 --><serviceandroid:name="com.tencent.android.tpush.service.XGPushService"android:exported="true"android:persistent="true"android:process=":xg_service_v2" /><!-- 【必須】 --><serviceandroid:name="com.tencent.android.tpush.rpc.XGRemoteService"android:exported="true"><intent-filter><!-- 【必須】 請(qǐng)修改為當(dāng)前APP名包.PUSH_ACTION,如demo的包名為:com.example.dc.xgdemo --><action android:name="com.example.dc.xgdemo.PUSH_ACTION" /></intent-filter></service><!-- 【必須】 請(qǐng)修改為APP的AccessId,“21”開(kāi)頭的10位數(shù)字,中間沒(méi)空格 --><meta-dataandroid:name="XG_V2_ACCESS_ID"android:value="2100250500" /><!-- 【必須】 請(qǐng)修改為APP的AccessKey,“A”開(kāi)頭的12位字符串,中間沒(méi)空格 --><meta-dataandroid:name="XG_V2_ACCESS_KEY"android:value="A95A2HLQ4K1K" /></application></manifest>step4.在activity中注冊(cè)信鴿public class MainActivity extends AppCompatActivity {Message m = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);XGPushConfig.enableDebug(this, true);setContentView(R.layout.activity_main);getToken();}/*** 獲取設(shè)備Token*/private void getToken() {Handler handler = new HandlerExtension(MainActivity.this);m = handler.obtainMessage();// 注冊(cè)接口XGPushManager.registerPush(getApplicationContext(), "單個(gè)賬號(hào)",new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("info", "+++ register push sucess. token:" + data);m.obj = "+++ register push sucess. token:" + data;m.sendToTarget();}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.i("info", "+++ register push fail. token:" + data+ ", errCode:" + errCode + ",msg:" + msg);m.obj = "+++ register push fail. token:" + data+ ", errCode:" + errCode + ",msg:" + msg;m.sendToTarget();}});}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);setIntent(intent);}@Overrideprotected void onPause() {super.onPause();XGPushManager.onActivityStoped(this);}private static class HandlerExtension extends Handler {WeakReference<MainActivity> mActivity;HandlerExtension(MainActivity activity) {mActivity = new WeakReference<>(activity);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);MainActivity theActivity = mActivity.get();if (theActivity == null) {theActivity = new MainActivity();}if (msg != null) {Log.i("info", "msg--->" + msg.obj.toString());}}}}setp5.如果要處理廣播創(chuàng)建一個(gè)廣播類(lèi)XGPushBaseReceiverpublic class MessageReceiver extends XGPushBaseReceiver{@Overridepublic void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {}@Overridepublic void onUnregisterResult(Context context, int i) {}@Overridepublic void onSetTagResult(Context context, int i, String s) {}@Overridepublic void onDeleteTagResult(Context context, int i, String s) {}/*** 消息透?jìng)? @param context* @param xgPushTextMessage*/@Overridepublic void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {if(xgPushTextMessage!=null){Log.e("info","message:"+xgPushTextMessage.toString());}}/*** 通知點(diǎn)擊回調(diào)* @param context* @param xgPushClickedResult*///可以直接在activity中獲取,下面這個(gè)方法是寫(xiě)在activity中的// protected void onResume() {// // TODO Auto-generated method stub// super.onResume();// XGPushClickedResult click = XGPushManager.onActivityStarted(this);// Log.d("TPush", "onResumeXGPushClickedResult:" + click);// if (click != null) { // 判斷是否來(lái)自信鴿的打開(kāi)方式// Toast.makeText(this, "通知被點(diǎn)擊:" + click.toString(),// Toast.LENGTH_SHORT).show();// }// }@Overridepublic void onNotifactionClickedResult(Context context, XGPushClickedResult xgPushClickedResult) {if(xgPushClickedResult!=null){Log.e("info","click:"+xgPushClickedResult.toString());}}/*** 通知的內(nèi)容* @param context* @param xgPushShowedResult*/@Overridepublic void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {if(xgPushShowedResult!=null){Log.e("info","notifaction:"+xgPushShowedResult.toString());}}}注冊(cè)廣播<!-- YOUR_PACKAGE_PATH.CustomPushReceiver需要改為自己的Receiver: --><receiver android:name=".receiver.MessageReceiver"android:exported="true" ><intent-filter><!-- 接收消息透?jìng)?--><action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" /><!-- 監(jiān)聽(tīng)注冊(cè)、反注冊(cè)、設(shè)置/刪除標(biāo)簽、通知被點(diǎn)擊等處理結(jié)果 --><action android:name="com.tencent.android.tpush.action.FEEDBACK" /></intent-filter></receiver>項(xiàng)目下載地址:http://download.csdn.net/detail/daidaishuiping/9752414新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注