讓你的音樂APP脫穎而出,更懂用戶,也更動人心、
場景
情景感知服務能帶來什么?
作為音樂發燒友,閑下來的時候總想打開App,享受沉浸在音樂中的放松。然而,App推薦的歌單經常沒法滿足我的需要,如果App能根據我當前的情景狀態,推送給我的歌曲恰好就是我當前最想聽的,那該多好啊~
什么是情景感知服務?
情景感知服務(Awareness Kit)能感知用戶當前的時間、地理位置、活動狀態、耳機狀態、天氣狀況、環境光、車載連接狀態、信標連接狀態等情景,並通過能常駐后台運行的圍欄能力向APP進行提醒,使APP能第一時間給用戶提供精准和貼心的服務。上述情景感知能力還在不斷擴充中,而且您可以自由組合這些感知能力,構建組合圍欄,從而讓APP的服務能力更加智能,更加精准。
在情景感知服務的支持下,App能給用戶帶來如下體驗
-
每次連接上耳機(有線耳機、藍牙耳機),手機通知欄會彈出音樂App通知,提示用戶時候是否要啟動音樂播放,或者是否要自動播放音樂
-
點擊通知,可以打開音樂APP,並展示用戶當前情境下最恰當的歌單
-
跑步開始,推薦節奏輕快歌單;跑步結束,推薦節奏緩場歌單
-
清晨起床,連接耳機,來點清新音樂;夜深人靜,連接耳機,讓靜心的音樂讓我舒緩
-
發動汽車,手機連接藍牙車載,音樂App自動切換成車載模式,並主動詢問是否播放各場景的行車音樂(白天、夜晚、雨天、晴天,推薦各不同)
-
不同的節日,應該有特別的節日音樂
同時,用戶還可以通過各種感知能力的組合圍欄,設置排除場景,避免給用戶過多打擾。
情景感知服務的優勢
無需用戶提前開啟App,用戶進入地理圍欄范圍后,即可后台激活App,觸發通知。
無懼App進程被系統殺死,通過圍欄服務,依然可接受到通知。
點擊通知,即可前台激活APP,點擊直接跳轉App推薦界面。
通過組合圍欄實現精准推送;也可避免在用戶不需要的場景提供無效通知,避免頻繁打擾。
開發前准備
Awareness Kit集成需要有如下3個關鍵步驟,可以參考華為開發者聯盟的文檔
- AppGallery Connect配置
- 集成HMS Awareness SDK
- 配置混淆腳本
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/awareness-preparation
代碼開發關鍵步驟
- 創建耳機圍欄
//創建一個感知耳機連接的圍欄,當耳機處於連接狀態時,此圍欄的狀態為true
AwarenessBarrier headsetBarrier = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
//創建一個PendingIntent,當圍欄狀態改變的時候,會觸發該PendingIntent,這里以發送廣播為例
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
//創建圍欄的標簽,后續可以通過標簽查詢或者刪除對應圍欄
String headsetBarrierLabel = "headset keeping connected label";
- 注冊圍欄
//把剛剛創建好的耳機圍欄和它對應的label,pendingIntent注冊給Awareness kit
Awareness.getBarrierClient(context).updateBarriers(new BarrierUpdateRequest.Builder()
.addBarrier(headsetBarrierLabel,headsetBarrier,pendingIntent).build())
.addOnSuccessListener(aVoid -> {
//注冊圍欄成功
Log.i(TAG,"add barrier success");
})
.addOnFailureListener(e -> {
//注冊圍欄失敗
Log.e(TAG,"add barrier failed");
e.printStackTrace();
});
- 創建廣播接收器監聽圍欄事件
//本示例中我們耳機圍欄的PendingIntent設置的是發送廣播,所以需要定義對應的廣播接收器來監聽圍欄的狀態
public final class HeadsetBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//圍欄的信息通過intent傳遞過來,我們通過Barrier.extract方法將其解析出來
BarrierStatus barrierState = BarrierStatus.extract(intent);
//通過BarrierStatus獲取label和圍欄的當前狀態
String label = barrierState.getBarrierLabel();
int status = barrierState.getPresentStatus();
if (status == BarrierStatus.TRUE && label.equals(headsetBarrierLabel)) {
//當圍欄狀態為true時,代表耳機處於連接狀態,這時就可以在通知欄推送相關消息
//send Notification....
}
}
}
定義完廣播接收器后別忘記注冊該廣播接收器,如果需要APP被殺后依然推送,可以把該接收器設置為靜態廣播接收器。
- 通過Awareness快照接口獲取當前情景狀態
在用戶點擊通知打開app后,可以通過Awareness各個能力的快照接口來獲取用戶當前的情景狀態以推薦不同歌單。
例如獲取時間情景狀態:
Awareness.getCaptureClient(context).getTimeCategories()
.addOnSuccessListener(timeIntervalsResponse -> {
TimeCategories categories = timeIntervalsResponse.getTimeCategories();
if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_HOLIDAY)) {
//當天是節假日,可推薦節假日歌單
}
if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_WEEKEND)) {
//當天是周末,可推薦周末歌單
}
if (categories.isTimeCategory(TimeBarrier.TIME_CATEGORY_NIGHT)) {
//當前是深夜,可推薦深夜歌單
}
})
.addOnFailureListener(e -> {
//獲取時間信息失敗
Log.e(TAG, "get Time Categories failed");
e.printStackTrace();
});
獲取用戶當前的活動狀態以推薦歌單:
Awareness.getCaptureClient(context).getBehavior()
.addOnSuccessListener(behaviorResponse -> {
BehaviorStatus behaviorStatus = behaviorResponse.getBehaviorStatus();
DetectedBehavior mostLikelyBehavior = behaviorStatus.getMostLikelyBehavior();
String str = "Most likely behavior is " + mostLikelyBehavior.getType();
})
.addOnFailureListener(e -> {
//獲取活動狀態失敗
Log.e(TAG, "Failed to get the behavior.", e);
});
獲取當前是否是連接車載藍牙:
int deviceType = 0; // 0 代表獲取的設備類型為車載藍牙
Awareness.getCaptureClient(this).getBluetoothStatus(deviceType)
.addOnSuccessListener(bluetoothStatusResponse -> {
BluetoothStatus bluetoothStatus = bluetoothStatusResponse.getBluetoothStatus();
int status = bluetoothStatus.getStatus();
if (status == BluetoothStatus.CONNECTED) {
//當前是連接車載藍牙,可將app切換為車載模式
}
})
.addOnFailureListener(e -> {
//獲取車載藍牙狀態失敗
Log.e(TAG, "Failed to get Bluetooth status.", e);
});
往期鏈接:一文搞懂文本識別、銀行卡識別、通用卡證識別、身份證識別
內容來源:https://developer.huawei.com/consumer/cn/forum/topicview?fid=18&tid=0201246052748810283&pid=0301246052748810253
原作者:Ascend