Android集成極光推送


要說學習極光推送,個人感覺官方文檔就非常好啦,但是沒法,人太懶啦,為了下次能夠快速的將極光推送集成到項目中,故結合之前開發的項目和官方文檔記錄下簡單的Android集成極光推送,在這之前,先上一張簡單的思維導圖吧,最近一直在研究思維導圖,感覺幫助還挺大的。

 

 

先從官方文檔中摘取出一些熟悉的名詞,推送目標主要分為四種:

廣播推送:向所有的注冊用戶發送一條廣播消息

標簽推送:根據屬性對用戶設置標簽分組,向群組用戶發送

別名推送:客戶端綁定用戶別名,向具體的單個用戶推送

用戶群體:根據JPush提供的多條件組合進行用戶群組划分,實時篩選實時推送

推送的消息形式也分為四種:

通知:指在手機的通知欄(狀態欄)上會顯示的一條通知信息。通知主要用於提示用戶的目的,應用於新聞內容、促銷活動、產品信息、版本更新提醒、訂單狀態提醒等多種場景

自定義形式:自定義消息不是通知,所以不會被SDK展示到通知欄上。其內容完全由開發者自己定義。自定義消息主要用於應用的內部業務邏輯。一條自定義消息推送過來,有可能沒有任何界面顯示。

富媒體:JPush支持開發者發送圖文並茂的通知,從而更好的傳達信息,帶來更豐富的用戶互動。JPush提供了5種模板,開發者可以通過填充模板的內容,發送landing page、彈窗、信息流形式的富媒體通知。開發者還可以直接通過URL發送預先編輯好的頁面。

本地通知本地通知API不依賴於網絡,無網條件下依舊可以觸發;本地通知的定時時間是自發送時算起的,不受中間關機等操作的影響。本地通知與網絡推送的通知是相互獨立的,不受保留最近通知條數上限的限制。本地通知適用於在特定時間發出的通知,如一些Todo和鬧鍾類的應用,在每周、每月固定時間提醒用戶回到應用查看任務

集成極光推送主要有兩種方式,一種是jcenter 自動集成,另外一種是手動集成,這里主要介紹下jcenter 自動集成,手動集成的可參考官方文檔

 1:我們先在module的gradle中添加依賴

1  compile 'cn.jiguang.sdk:jpush:3.0.3'  // 此處以JPush 3.0.3 版本為例。
2  compile 'cn.jiguang.sdk:jcore:1.1.1'  // 此處以JCore 1.1.1 版本為例。

2:確認android studio的 Project 根目錄的主 gradle 中配置了jcenter支持。(新建project默認配置就支持)

 1 buildscript {
 2     repositories {
 3         jcenter()
 4     }
 5     ......
 6 }
 7 
 8 allprojets {
 9     repositories {
10         jcenter()
11     }
12 }

3:在module的gradle中的defaultconfig添加AndroidManifest的替換變量

 1    ndk {
 2             //選擇要添加的對應cpu類型的.so庫。
 3             abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
 4             // 還可以添加 'x86', 'x86_64', 'mips', 'mips64'
 5         }
 6 
 7         manifestPlaceholders = [
 8                 JPUSH_PKGNAME: applicationId,
 9                 JPUSH_APPKEY : "4f44ddfa7aa983a7df72b5b3", //JPush上注冊的包名對應的appkey.
10                 JPUSH_CHANNEL: "developer-default", //用戶渠道統計的渠道名稱
11         ]

4:在AndroidManifest中添加JPusService並配置android:process參數將PushService放在其他進程中:

 1  <!-- Required SDK 核心功能-->
 2         <!-- 可配置android:process參數將PushService放在其他進程中 -->
 3         <service
 4             android:name="cn.jpush.android.service.PushService"
 5             android:process=":pushcore"
 6             android:exported="false">
 7             <intent-filter>
 8                 <action android:name="cn.jpush.android.intent.REGISTER" />
 9                 <action android:name="cn.jpush.android.intent.REPORT" />
10                 <action android:name="cn.jpush.android.intent.PushService" />
11                 <action android:name="cn.jpush.android.intent.PUSH_TIME" />
12             </intent-filter>
13         </service>

至此,集成極光推送完畢,那我們怎么使用呢?其實你可以下一個官方的Demo JPushExample研究研究

1:首先我們要在Application中設置JPush調試模式和初始化SDK

 1 /**
 2  * Created by kebinran on 2017/7/20.
 3  */
 4 
 5 public class APP extends Application {
 6     @Override
 7     public void onCreate() {
 8         super.onCreate();
 9         //設置調試模式
10         JPushInterface.setDebugMode(true);
11         //init 初始化SDK
12         JPushInterface.init(this);
13     }
14 }

2:自定義的廣播接收器並編寫邏輯代碼,你可以更快捷的從JPushExample中復制響應的代碼在上面修改即可

  1 package com.example.kebinran.jpushdemo;
  2 
  3 import android.content.BroadcastReceiver;
  4 import android.content.Context;
  5 import android.content.Intent;
  6 import android.os.Bundle;
  7 import android.support.v4.content.LocalBroadcastManager;
  8 import android.text.TextUtils;
  9 
 10 import org.json.JSONException;
 11 import org.json.JSONObject;
 12 
 13 import java.util.Iterator;
 14 
 15 import cn.jpush.android.api.JPushInterface;
 16 
 17 /**
 18  * 自定義接收器
 19  * 
 20  * 如果不定義這個 Receiver,則:
 21  * 1) 默認用戶會打開主界面
 22  * 2) 接收不到自定義消息
 23  */
 24 public class MyReceiver extends BroadcastReceiver {
 25     private static final String TAG = "JIGUANG-Example";
 26 
 27     @Override
 28     public void onReceive(Context context, Intent intent) {
 29         try {
 30             Bundle bundle = intent.getExtras();
 31             Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
 32 
 33             if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
 34                 String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
 35                 Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
 36                 //send the Registration Id to your server...
 37 
 38             } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
 39                 Logger.d(TAG, "[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
 40                 processCustomMessage(context, bundle);
 41 
 42             } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
 43                 Logger.d(TAG, "[MyReceiver] 接收到推送下來的通知");
 44                 int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
 45                 Logger.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
 46 
 47             } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
 48                 Logger.d(TAG, "[MyReceiver] 用戶點擊打開了通知");
 49 
 50                 //打開自定義的Activity
 51                 Intent i = new Intent(context, MainActivity.class);
 52                 i.putExtras(bundle);
 53                 //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 54                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
 55                 context.startActivity(i);
 56 
 57             } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
 58                 Logger.d(TAG, "[MyReceiver] 用戶收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
 59                 //在這里根據 JPushInterface.EXTRA_EXTRA 的內容處理代碼,比如打開新的Activity, 打開一個網頁等..
 60 
 61             } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
 62                 boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
 63                 Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
 64             } else {
 65                 Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
 66             }
 67         } catch (Exception e){
 68 
 69         }
 70 
 71     }
 72 
 73     // 打印所有的 intent extra 數據
 74     private static String printBundle(Bundle bundle) {
 75         StringBuilder sb = new StringBuilder();
 76         for (String key : bundle.keySet()) {
 77             if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
 78                 sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
 79             }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
 80                 sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
 81             } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
 82                 if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
 83                     Logger.i(TAG, "This message has no Extra data");
 84                     continue;
 85                 }
 86 
 87                 try {
 88                     JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
 89                     Iterator<String> it =  json.keys();
 90 
 91                     while (it.hasNext()) {
 92                         String myKey = it.next().toString();
 93                         sb.append("\nkey:" + key + ", value: [" +
 94                                 myKey + " - " +json.optString(myKey) + "]");
 95                     }
 96                 } catch (JSONException e) {
 97                     Logger.e(TAG, "Get message extra JSON error!");
 98                 }
 99 
100             } else {
101                 sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
102             }
103         }
104         return sb.toString();
105     }
106     
107     //send msg to MainActivity
108     private void processCustomMessage(Context context, Bundle bundle) {
109         if (MainActivity.isForeground) {
110             String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
111             String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
112             Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
113             msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
114             if (!ExampleUtil.isEmpty(extras)) {
115                 try {
116                     JSONObject extraJson = new JSONObject(extras);
117                     if (extraJson.length() > 0) {
118                         msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
119                     }
120                 } catch (JSONException e) {
121 
122                 }
123 
124             }
125             LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
126         }
127 
128     }
129 }

別忘了在AndroidManifest中聲明

 1   <!-- User defined.  For test only  用戶自定義的廣播接收器-->
 2         <receiver
 3             android:name=".MyReceiver"
 4             android:enabled="true"
 5             android:exported="false">
 6             <intent-filter>
 7                 <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用戶注冊SDK的intent-->
 8                 <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required  用戶接收SDK消息的intent-->
 9                 <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required  用戶接收SDK通知欄信息的intent-->
10                 <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required  用戶打開自定義通知欄的intent-->
11                 <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收網絡變化 連接/斷開 since 1.6.3 -->
12                 <category android:name="com.example.kebinran.jpushdemo" />
13             </intent-filter>
14         </receiver>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM