體驗個推推送,感覺Android推送的魅力

原博客轉自:http://blog.csdn.net/dengyanyi11/article/details/44280699html


消息推送在android應用中已經不勝枚舉,足以體現它的重要性。今天我來和你們一塊兒學習一下目前最主流的推送之一 - 個推推送 。java

      1,咱們先在個推官網註冊一個本身的帳號,而後在應用管理裏登記本身的應用(應用標識記得填本身項目的packageName),拿到AppID,AppKey,AppSecret,MasterSecret(咱們在後面須要用到這些)。android

       請見圖:
git

    2,以後咱們在接入嚮導裏下載個推的SDK(GETUI_ANDROID_SDK),解壓發現API文檔,Demo工程,資源文件。github

       請見圖:服務器

       

     3,將資源文件導入咱們工程的lib中。 app

      請見圖:ide

        

         4,在AndroidManifest.xml 中加入聲明服務學習

[html] view plaincopy測試

  1. <pre name="code" class="html" style="color: rgb(35, 35, 35); font-size: 14px; line-height: 25px;"> <!-- 個推SDK配置開始 -->  

  2.         <!-- 配置的第三方參數屬性 -->  

  3.         <meta-data  

  4.             android:name="PUSH_APPID"  

  5.             android:value="dKHYjw6bxB7VuuMp7n9uM9" /> <!-- APPID -->  

  6.         <meta-data  

  7.             android:name="PUSH_APPKEY"  

  8.             android:value="PesVQ3QhydATvduzLsj8E5" /> <!-- APPKEY -->  

  9.         <meta-data  

  10.             android:name="PUSH_APPSECRET"  

  11.             android:value="CsJEgWLA5Z5wxkdiZjkVR2" /> <!-- APPSECRET -->  

  12.         <meta-data  

  13.             android:name="PUSH_GROUPID"  

  14.             android:value="" /><span style="font-family: 'Microsoft Yahei', 宋體, 黑體;"><!-- 這裏能夠不填 --></span>  

  15.   

  16.         <service  

  17.             android:name="com.igexin.sdk.PushService"  

  18.             android:exported="true"  

  19.             android:label="NotificationCenter"  

  20.             android:process=":pushservice" >  

  21.         </service>  

  22.   

  23.         <receiver android:name="com.igexin.sdk.PushReceiver" >  

  24.             <intent-filter>  

  25.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  

  26.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  

  27.                 <action android:name="android.intent.action.USER_PRESENT" />  

  28.                 <action android:name="com.igexin.sdk.action.refreshls" />  

  29.             </intent-filter>  

  30.         </receiver>  

  31.         <receiver  

  32.             android:name="com.igexin.sdk.PushManagerReceiver"  

  33.             android:exported="false" >  

  34.             <intent-filter>  

  35.                 <action android:name="com.igexin.sdk.action.pushmanager" />  

  36.             </intent-filter>  

  37.         </receiver>  

  38.   

  39.         <activity  

  40.             android:name="com.igexin.sdk.PushActivity"  

  41.             android:excludeFromRecents="true"  

  42.             android:exported="false"  

  43.             android:process=":pushservice"  

  44.             android:taskAffinity="com.igexin.sdk.PushActivityTask"  

  45.             android:theme="@android :style/Theme.Translucent.NoTitleBar" >  

  46.         </activity>  

  47.         <!-- 配置彈框activity -->  

  48.         <activity  

  49.             android:name="com.igexin.getuiext.activity.GetuiExtActivity"  

  50.             android:configChanges="orientation|keyboard|keyboardHidden"  

  51.             android:excludeFromRecents="true"  

  52.             android:exported="false"  

  53.             android:process=":pushservice"  

  54.             android:taskAffinity="android.task.myServicetask"  

  55.             android:theme="@android :style/Theme.Translucent.NoTitleBar" />  

  56.   

  57.         <receiver  

  58.             android:name="com.igexin.getuiext.service.PayloadReceiver"  

  59.             android:exported="false" >  

  60.             <intent-filter>  

  61.                 <!-- 這個com.igexin.sdk.action.7fjUl2Z3LH6xYy7NQK4ni4固定,不能修改 -->  

  62.                 <action android:name="com.igexin.sdk.action.7fjUl2Z3LH6xYy7NQK4ni4" />  

  63.                 <!-- android:name="com.igexin.sdk.action.第三方的appId" -->  

  64.                 <action android:name="com.igexin.sdk.action.dKHYjw6bxB7VuuMp7n9uM9" />  

  65.             </intent-filter>  

  66.         </receiver>  

  67.   

  68.         <service  

  69.             android:name="com.igexin.getuiext.service.GetuiExtService"  

  70.             android:process=":pushservice" />  

  71.         <!-- 個推download模塊配置 -->  

  72.         <service  

  73.             android:name="com.igexin.download.DownloadService"  

  74.             android:process=":pushservice" />  

  75.   

  76.         <receiver android:name="com.igexin.download.DownloadReceiver" >  

  77.             <intent-filter>  

  78.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  

  79.             </intent-filter>  

  80.         </receiver<span style="font-family: 'Microsoft Yahei', 宋體, 黑體;">></span>  

[html] view plaincopy

  1. <span style="font-family: 'Microsoft Yahei', 宋體, 黑體;"></span><pre name="code" class="html" style="color: rgb(35, 35, 35); font-size: 14px; line-height: 25px;"> <span style="white-space:pre">  </span>    <!-- android:authorities="downloads.第三方包名" -->  


[html] view plaincopy

  1.         <provider  

  2.             android:name="com.igexin.download.DownloadProvider"  

  3.             android:authorities="downloads.com.dengyy.pushtest"  

  4.             android:process=":pushservice" />  

[html] view plaincopy

  1.         <!-- 透傳消息Receiver -->  

  2.         <receiver  

  3.             android:name="com.dengyy.pushtest.PushDemoReceiver"  

  4.             android:exported="false" >  

  5.             <intent-filter>  

  6.                 <!-- action android:name="com.igexin.sdk.action.第三方的APPID" -->  

  7.                 <action android:name="com.igexin.sdk.action.dKHYjw6bxB7VuuMp7n9uM9" />  

  8.             </intent-filter>  

  9.         </receiver>  


      5,加入權限

[html] view plaincopy

  1. <!-- 個推權限 -->  

  2. <uses-permission android:name="android.permission.INTERNET" />  

  3. <uses-permission android:name="android.permission.READ_PHONE_STATE" />  

  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

  5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  

  6. <uses-permission android:name="android.permission.WAKE_LOCK" />  

  7. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  

  8. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

  9. <uses-permission android:name="android.permission.VIBRATE" />  

  10. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

  11. <uses-permission android:name="getui.permission.GetuiService" />  

  12. <uses-permission android:name="android.permission.GET_TASKS" />  

  13. <!-- 自定義權限 -->  

  14. <permission  

  15.     android:name="getui.permission.GetuiService"  

  16.     android:protectionLevel="normal" >  

  17. </permission>  


      6,以後咱們在MainActivity裏啓動初始化SDK。

[java] view plaincopy

  1. package com.dengyy.pushtest;  

  2.   

  3. import android.app.Activity;  

  4. import android.os.Bundle;  

  5. import android.view.Window;  

  6. import android.widget.TextView;  

  7.   

  8. import com.igexin.sdk.PushManager;  

  9.   

  10. public class MainActivity extends Activity {  

  11.     public static TextView tView = null;  

  12.     public static TextView tLogView = null;  

  13.   

  14.     @Override  

  15.     protected void onCreate(Bundle savedInstanceState) {  

  16.         super.onCreate(savedInstanceState);  

  17.         requestWindowFeature(Window.FEATURE_NO_TITLE);  

  18.         setContentView(R.layout.activity_main);  

  19.           

  20.         //初始化SDK  

  21.         PushManager.getInstance().initialize(this.getApplicationContext());  

  22.           

  23.     }  

  24. }  

      注意:PushManager.getInstance().initialize(this.getApplicationContext()) 通常在Activity或Service類內調用,能夠在Activity的onCreate()方法中調用。不建議在Application繼承類中調用。

       

      7,將官方Demo裏PushDemoReceiver類複製到本身的項目中

      如下是我加入了一些容易理解的註釋的代碼:

[java] view plaincopy

  1. package com.dengyy.pushtest;  

  2.   

  3. import android.content.BroadcastReceiver;  

  4. import android.content.Context;  

  5. import android.content.Intent;  

  6. import android.os.Bundle;  

  7. import android.util.Log;  

  8.   

  9. import com.igexin.sdk.PushConsts;  

  10. import com.igexin.sdk.PushManager;  

  11.   

  12. public class PushDemoReceiver extends BroadcastReceiver {  

  13.   

  14.     @Override  

  15.     public void onReceive(Context context, Intent intent) {  

  16.         Bundle bundle = intent.getExtras();  

  17.         Log.d("GetuiSdkDemo""onReceive() action=" + bundle.getInt("action"));  

  18.         switch (bundle.getInt(PushConsts.CMD_ACTION)) {  

  19.   

  20.         case PushConsts.GET_MSG_DATA:  

  21.             // 獲取透傳數據  

  22.             // String appid = bundle.getString("appid");  

  23.             byte[] payload = bundle.getByteArray("payload");  

  24.   

  25.             String taskid = bundle.getString("taskid");  

  26.             String messageid = bundle.getString("messageid");  

  27.   

  28.             // smartPush第三方回執調用接口,actionid範圍爲90000-90999,可根據業務場景執行  

  29.             // 透傳消息,根據事件id改變事件  

  30.             boolean result = PushManager.getInstance().sendFeedbackMessage(  

  31.                     context, taskid, messageid, 90001);  

  32.               

  33.             if(result){  

  34.                 Intent intentTemp = new Intent(context.getApplicationContext(),ResultActivity.class);  

  35.                 intentTemp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);   

  36.                 context.getApplicationContext().startActivity(intentTemp);  

  37.             }  

  38.               

  39.             if (payload != null) {  

  40.                 String data = new String(payload);  

  41.   

  42.                 Log.d("GetuiSdkDemo""Got Payload:" + data);  

  43.                 if (MainActivity.tLogView != null)  

  44.                     MainActivity.tLogView.append(data + "\n");  

  45.             }  

  46.             break;  

  47.         case PushConsts.GET_CLIENTID:  

  48.             // 獲取ClientID(CID)  

  49.             // 第三方應用須要將CID上傳到第三方服務器,而且將當前用戶賬號和CID進行關聯,以便往後經過用戶賬號查找CID進行消息推送  

  50.             String cid = bundle.getString("clientid");  

  51.             if (MainActivity.tView != null)  

  52.                 MainActivity.tView.setText(cid);  

  53.             break;  

  54.         case PushConsts.THIRDPART_FEEDBACK:  

  55.             /* 

  56.              * String appid = bundle.getString("appid"); String taskid = 

  57.              * bundle.getString("taskid"); String actionid = 

  58.              * bundle.getString("actionid"); String result = 

  59.              * bundle.getString("result"); long timestamp = 

  60.              * bundle.getLong("timestamp"); 

  61.              *  

  62.              * Log.d("GetuiSdkDemo", "appid = " + appid); Log.d("GetuiSdkDemo", 

  63.              * "taskid = " + taskid); Log.d("GetuiSdkDemo", "actionid = " + 

  64.              * actionid); Log.d("GetuiSdkDemo", "result = " + result); 

  65.              * Log.d("GetuiSdkDemo", "timestamp = " + timestamp); 

  66.              */  

  67.             break;  

  68.         default:  

  69.             break;  

  70.         }  

  71.     }  

  72. }  

      

      8,到了這裏,咱們能夠開始測試了。讓消息飛起來吧!


     除此以外,咱們還能夠自定義消息推送,試試看吧


     個人Demo代碼能夠點這裏下載:https://github.com/toeii/GeTuiPushDemo

相關文章
相關標籤/搜索