HMS Core地理圍欄能力助你實現指定範圍人群的精準訊息推送

HMSCore發表於2022-05-07

精準推送是移動端產品留存階段的主要運營手段,精準推送常常會與使用者畫像緊密結合,針對使用者的喜好、畫像,採用不同策略,但基於使用者所屬區域推送訊息卻很難實現。目前市面上大多數第三方訊息推送服務商,在系統未深度定製的情況下,通常不支援將推送人群範圍精確到某個商圈或較小的區域,而地理圍欄技術可以很好地彌補這一點。地理圍欄就是用一個虛擬的柵欄圍出一個虛擬地理邊界,當手機進入、離開或在這個圍起來的特定地理區域內活動時,手機可以自動接收通知和警告訊息。將地理圍欄和訊息推送相結合,即可實現對特定區域人群的精準訊息推送。

舉個例子,一款旅遊出行類App想在江蘇推廣其門票業務,他可以針對南京、蘇州等城市的熱門旅遊景點劃定地理圍欄,當目標受眾在特定時間段到達某個旅遊景點附近時,將會收到一條特定訊息推送“XX景點門票優惠券已放入賬戶中,立即領取>>”在使用者有購買XX景點門票需求時應景推送優惠資訊,讓使用者無法拒絕。

實現方法

華為定位服務地理圍欄能力結合推送服務訊息推送能力,就可實現對指定範圍人群的精準訊息推送。通過設定特定的區域,可以檢測使用者的事件狀態,例如他們何時進入、離開或停留在該區域,一旦滿足觸發條件,使用者裝置將實時收到訊息推送。即使應用不在後臺執行,也可以在使用者裝置上傳遞和顯示訊息,訊息傳遞率可達99%。

效果展示:

  1. 在測試裝置上安裝演示應用。
  2. 啟動演示應用程式,點選地理圍欄螢幕上的新增地理圍欄,然後設定相關引數以建立地理圍欄。
  3. 等待地理圍欄被觸發。
  4. 在觸發地理圍欄時檢查收到的訊息。

開發步驟

  1. 配置SDK的Maven倉地址。
  2. (Android Studio的程式碼庫配置在Gradle外掛7.0以下版本、7.0版本和7.1及以上版本有所不同。請根據您當前的Gradle外掛版本,選擇對應的配置過程。這裡以7.1版本為例)

a) 在“buildscript > dependencies”中增加agcp外掛配置。

buildscript {
i.        dependencies {
ii.            ...
iii.            // 增加agcp外掛配置,推薦您使用最新版本的agcp外掛。
iv.            classpath 'com.huawei.agconnect:agcp:1.6.0.300'
    }
v.    }

b) 專案級“settings.gradle”檔案,配置HMS Core SDK的Maven倉地址。

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        // 配置HMS Core SDK的Maven倉地址。
        maven { url 'https://developer.huawei.com/repo/' }
    }
}
dependencyResolutionManagement {
    ...
    repositories {
        google()
        mavenCentral()
        // 配置HMS Core SDK的Maven倉地址。
        maven { url 'https://developer.huawei.com/repo/' }
    }
}

2.在“dependencies ”中新增如下編譯依賴。

//應用級的“build.gradle”檔案
dependencies {
    implementation 'com.huawei.hms:location: 6.4.0.300'
    implementation 'com.huawei.hms:push: 6.3.0.304'
}
  1. 在 AndroidManifest.xml 檔案中宣告系統許可權。

因華為定位服務採用GNSS、Wi-Fi、基站等多種混合定位模式進行定位,賦予您的應用程式快速、精準地獲取使用者位置資訊的能力,需要用到網路,精確的位置許可權,粗略的位置許可權如果您需要應用程式在後臺執行時也具備持續定位能力,需要在Manifest檔案中申請ACCESS_BACKGROUND_LOCATION許可權:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARES_LOCATION" />

注:由於ACCESS_FINE_LOCATION,WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE是危險的系統許可權,因此,您需要動態的申請這些許可權。如果許可權不足,Location Service將會拒絕為您的應用開啟定位。

關鍵程式碼說明

程式碼檔案路徑: com.huawei.hmssample2.geofence\GeoFenceActivity.java

如果您的應用需要整合圍欄Service實現服務推送,僅僅需要參考將GeoFenceActivity整合到您的應用中在接收圍欄回撥的同時發出推送的廣播,即可實現。

  1. 觸發地理圍欄。

a) 根據需要建立地理圍欄和地理圍欄組,並設定相應的引數,比如圍欄半徑,觸發時間等。

if (checkStyle(geofences, data.uniqueId) == false) {
    LocationLog.d("GeoFenceActivity", "not unique ID!");
    LocationLog.i("GeoFenceActivity", "addGeofence failed!");
    return;
}
geoBuild.setRoundArea(data.latitude, data.longitude, data.radius);
geoBuild.setUniqueId(data.uniqueId);
geoBuild.setConversions(data.conversions);
geoBuild.setValidContinueTime(data.validContinueTime);
geoBuild.setDwellDelayTime(data.dwellDelayTime);
geoBuild.setNotificationInterval(data.notificationInterval);
geofences.add(geoBuild.build());
LocationLog.i("GeoFenceActivity", "addGeofence success!");

b) 設定好相關引數之後通過intent註冊廣播

GeofenceRequest.Builder geofenceRequest = new GeofenceRequest.Builder();
geofenceRequest.createGeofenceList(GeoFenceData.returnList());
if (trigger.getText() != null) {
    int trigGer = Integer.parseInt(trigger.getText().toString());
    geofenceRequest.setInitConversions(trigGer);
    LocationLog.d(TAG, "trigger is " + trigGer);
} else {
    geofenceRequest.setInitConversions(5);
    LocationLog.d(TAG, "default trigger is 5");
}

final PendingIntent pendingIntent = getPendingIntent();
try {
    geofenceService.createGeofenceList(geofenceRequest.build(), pendingIntent)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    LocationLog.i(TAG, "add geofence success!");
                    setList(pendingIntent, GeoFenceData.getRequestCode(), GeoFenceData.returnList());
                    GeoFenceData.createNewList();
                } else {
                    // Get the status code for the error and log it using a user-friendly message.
                    LocationLog.w(TAG, "add geofence failed : " + task.getException().getMessage());
                }
            }
        });
} catch (Exception e) {
    LocationLog.i(TAG, "add geofence error:" + e.getMessage());
}
private PendingIntent getPendingIntent() {
    Intent intent = new Intent(this, GeoFenceBroadcastReceiver.class);
    intent.setAction(GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION);
    Log.d(TAG, "new request");
    GeoFenceData.newRequest();
    return PendingIntent.getBroadcast(this, GeoFenceData.getRequestCode(), intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
}
  1. 觸發訊息推送。
在GeoFenceBroadcastReceiver的onReceive接收到圍欄觸發成功提示後傳送推送通知,在通知欄接收通知並展示。
GeofenceData geofenceData = GeofenceData.getDataFromIntent(intent);
if (geofenceData != null) {
    int errorCode = geofenceData.getErrorCode();
    int conversion = geofenceData.getConversion();
    ArrayList<Geofence> list = (ArrayList<Geofence>) geofenceData.getConvertingGeofenceList();
    Location myLocation = geofenceData.getConvertingLocation();
    boolean status = geofenceData.isSuccess();
    sb.append("errorcode: " + errorCode + next);
    sb.append("conversion: " + conversion + next);
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            sb.append("geoFence id :" + list.get(i).getUniqueId() + next);
        }
    }
    if (myLocation != null) {
        sb.append("location is :" + myLocation.getLongitude() + " " + myLocation.getLatitude() + next);
    }
    sb.append("is successful :" + status);
    LocationLog.i(TAG, sb.toString());
    Toast.makeText(context, "" + sb.toString(), Toast.LENGTH_LONG).show();
    //
    new PushSendUtils().netSendMsg(sb.toString());
}

注意:使用示例程式碼建立的地理圍欄將觸發轉化型別 1 和 4 的兩個回撥。一個在使用者進入地理圍欄時觸發,另一個在使用者停留在地理圍欄內時觸發。如果在程式碼中將 Trigger 設定為 7,則會配置所有方案(包括進入、停留和離開地理圍欄)的回撥。

完成以上開發步驟就可以完成地理圍欄推送功能,實現在指定區域內推送訊息通知實現精準營銷。

瞭解更多詳情>>

訪問華為開發者聯盟官網
獲取開發指導文件
華為移動服務開源倉庫地址:GitHubGitee

關注我們,第一時間瞭解 HMS Core 最新技術資訊~

相關文章