æ¶Âæ¯æ¨éÂÂä½Â为Appè¿ÂèÂ¥æ¥常使ç¨çÂÂç¨æ·ä¿Âæ´»åÂÂåŒÂÂæÂÂ段ï¼Âæ¯ä¸Âç¨æ·建ç«ÂæÂÂç»ÂäºÂå¨åÂÂè¿ÂæÂ¥çÂÂè¯好æ¹å¼ÂãÂÂæ¨éÂÂæÂÂå¡ï¼ÂPush Kitï¼Âæ¯åÂÂ为æÂÂä¾ÂçÂÂæ¶Âæ¯æ¨éÂÂå¹³å°ï¼Â建ç«ÂäºÂä»ÂäºÂ端å°ç»Â端çÂÂæ¶Âæ¯æ¨éÂÂéÂÂéÂÂï¼Âæ¾ÂÂæ¨å¨ä»Âç»ÂHarmonyOSï¼ÂJavaï¼ÂçÂÂæÂÂÃ¥ÂÂè½åÂÂæ¥堥æÂÂ导ãÂÂ
HarmonyOSï¼ÂJavaï¼ÂçÂÂæ¬主è¦ÂÃ¥ÂÂè½为根æ®tokenæ¨éÂÂéÂÂçÂ¥æ ÂÃ¥ÂÂéÂÂä¼ æ¶Âæ¯ãÂÂHarmonyOS Java SDK为æ¨çÂÂHarmonyOSåºÂç¨å¼ÂÃ¥ÂÂæÂÂä¾Âæ¨éÂÂæ¶Âæ¯ç¸堳çÂÂæÂ¥å£ï¼ÂéÂÂç¨äºÂæÂÂæºåÂÂå¹³æ¿ãÂÂ
æ ¹æ®Push Tokenæ¨éÂÂæ¶Âæ¯
æ¨å¯以è¾Âå ¥ç®æ Âç¨æ·çÂÂPush TokenæÂ¥æ¨éÂÂæ¶Âæ¯ï¼Âä¸Â次æÂÂå¤Âå¯填1000个Push TokenãÂÂ
éÂÂä¼ æ¶Âæ¯
éÂÂä¼ æ¶Âæ¯æ¯ç±客æ·端åºÂç¨è´Âè´£å¤ÂçÂÂçÂÂæ¶Âæ¯ãÂÂç»Â端设å¤Âæ¶å°PushäºÂ端åÂÂéÂÂçÂÂæ°æ®æÂÂæÂÂ令åÂÂä¸Âç´æÂ¥å±Â示ï¼ÂèÂÂæ¯å°Âæ°æ®传éÂÂç»ÂåºÂç¨ï¼Âç±åºÂç¨解æÂÂå 容ï¼Â并触åÂÂç¸堳å¨ä½Âï¼Âå¦Â跳转ç½Â页ãÂÂåºÂç¨å 页é¢çÂÂçÂÂï¼ÂãÂÂæ¨å¯以èªå®Âä¹Âæ¶Âæ¯样å¼Âï¼Âä»ÂèÂÂæ´é«ÂæÂÂçµ活å°æ¨éÂÂæ¶Âæ¯ãÂÂ
éÂÂä¼ æ¶Âæ¯çÂÂå°达çÂÂÃ¥ÂÂAndroidç³»ç»ÂÃ¥ÂÂåºÂç¨æ¯å¦驻çÂÂå¨åÂÂå°影åÂÂï¼Âæ¨éÂÂæÂÂå¡ä¸Âä¿Âè¯ÂéÂÂä¼ æ¶Âæ¯çÂÂé«Âå°达çÂÂãÂÂ
éÂÂä¼ æ¶Âæ¯çÂÂ常ç¨åºæ¯ï¼ÂVoIPå¼å«ãÂÂè¯Âé³æÂÂæÂ¥ãÂÂ好åÂÂäºÂå¨éÂÂçÂ¥çÂÂãÂÂ
å¼ÂÃ¥ÂÂÃ¥ÂÂå¤Â
å ·ä½Âå¼ÂÃ¥ÂÂÃ¥ÂÂå¤Â请åÂÂèÂÂå®Âç½ÂãÂÂ
åºÂç¨å¼ÂÃ¥ÂÂ
è·åÂÂPush Token
Tokenæ¯æ¨éÂÂ令çÂÂï¼Âæ¯Â个设å¤Âä¸ÂçÂÂæ¯Â个åºÂç¨çÂÂTokené½æ¯å¯ä¸ÂÃ¥ÂÂå¨çÂÂï¼Â客æ·端è°Âç¨getTokenæ¹æ³ÂÃ¥ÂÂPushæÂÂå¡端请æ±ÂåºÂç¨çÂÂTokenï¼Âæ¨å¯以根æ®PushæÂÂå¡端è¿ÂÃ¥ÂÂçÂÂTokenÃ¥ÂÂåºÂç¨æ¨éÂÂæ¶Âæ¯ãÂÂå½ÂgetTokenæ¹æ³Âè¿ÂÃ¥ÂÂ为空æ¶ï¼Âå¯éÂÂè¿ÂonNewTokenæ¹æ³Âè·åÂÂTokenå¼ãÂÂ
建议å°ÂPush Tokenä¸ÂæÂ¥å°æ¨èª己çÂÂåºÂç¨æÂÂå¡å¨ï¼Â并å®Âæ¶æ´æ°TokenÃ¥ÂÂ表ãÂÂæ¨å¯以è°Âç¨ä¸Âè¡Âæ¶Âæ¯APIï¼Âæ ¹æ®è¿ÂäºÂTokenæ¹éÂÂæ¨éÂÂæ¶Âæ¯ãÂÂ
- Ã¥ÂÂ建ä¸Â个æ°çÂÂ线ç¨Âï¼Â并è°Âç¨getTokenæ¹æ³Âè·åÂÂPush Tokenï¼Â建议å¨åºÂç¨å¯å¨åÂÂçÂÂé¦Â个Abilityä¸Âè°Âç¨getTokenæ¹æ³Âï¼ÂãÂÂ
public class TokenAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234,
"TokenAbilitySlice");
private void getToken() {
// Ã¥ÂÂ建æ°线ç¨Â
new Thread("getToken") {
@Override
public void run() {
try {
// ä»Âagconnect-services.jsonæÂÂ件ä¸Â读åÂÂclient/app_id
String appId = "your APP_ID";
// è¾ÂÃ¥Â
Â¥tokenæ Âè¯Â"HCM"
String tokenScope = "HCM";
// è·åÂÂPush Token
String token = HmsInstanceId.getInstance(getAbility().getAbilityPackage(), TokenAbilitySlice.this).getToken(appId, tokenScope);
} catch (ApiException e) {
// è·åÂÂPush Token失败æ¶ï¼ÂæÂÂå°éÂÂ误ç Â
HiLog.error(LABEL_LOG, "get token failed, the error code is %{public}d", e.getStatusCode());
}
}
}.start();
}
}
- å¨æ¨çÂÂserviceï¼Â已继æ¿HmsMessageServiceï¼Âä¸Âï¼Âè¦ÂÃ¥ÂÂonNewTokenæ¹æ³Âï¼Âå½ÂTokenÃ¥ÂÂçÂÂÃ¥ÂÂÃ¥ÂÂæ¶以onNewTokenæ¹æ³Âè¿ÂÃ¥ÂÂãÂÂ
public class DemoHmsMessageServiceAbility extends HmsMessageService {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234, "DemoHmsMessageServiceAbility");
@Override
// è·åÂÂToken
public void onNewToken(String token) {
HiLog.info(LABEL_LOG, "onNewToken called, token:%{public}s", token);
}
@Override
// è·åÂÂToken失败ï¼ÂæÂÂå°éÂÂ误ç Â
public void onTokenError(Exception exception) {
HiLog.error(LABEL_LOG, "get onNewtoken error, error code is %{public}d", ((ZBaseException)exception).getErrorCode());
}
}
è·åÂÂéÂÂä¼ æ¶Âæ¯æ°æ®
å¨æ¨çÂÂserviceï¼Â已继æ¿HmsMessageServiceï¼Âä¸Âï¼Âè¦ÂÃ¥ÂÂonMessageReceivedæ¹æ³Âï¼Âåªè¦Âæ¨åÂÂéÂÂéÂÂä¼ æ¶Âæ¯è³ç»Â端设å¤Âï¼Âé½ä¼Âè·å¾ÂéÂÂä¼ æ¶Âæ¯çÂÂå 容ãÂÂ
public class DemoHmsMessageServiceAbility extends HmsMessageService {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234,
"DemoHmsMessageServiceAbility");
@Override
public void onMessageReceived(ZRemoteMessage message) {
// æÂÂå°æ¶Âæ¯çÂÂÃ¥ÂÂ
容åÂÂ段
HiLog.info(LABEL_LOG, "get token, %{public}s", message.getToken());
HiLog.info(LABEL_LOG, "get data, %{public}s", message.getData());
ZRemoteMessage.Notification notification = message.getNotification();
if (notification != null) {
HiLog.info(LABEL_LOG, "get title, %{public}s", notification.getTitle());
HiLog.info(LABEL_LOG, "get body, %{public}s", notification.getBody());
}
}
}
Ã¥ÂÂéÂÂæ¶Âæ¯
⢠æ¨å¯以ç»å½ÂAppGallery Connectç½Âç«ÂÃ¥ÂÂéÂÂæ¶Âæ¯ï¼Â详æ 请åÂÂè§ÂHarmonyOSåºÂç¨æ¨éÂÂæ¶Âæ¯ãÂÂ
⢠æ¨å¯以使ç¨æÂÂå¡端APIÃ¥ÂÂåºÂç¨æ¨éÂÂæ¶Âæ¯ï¼Â
- æ¨çÂÂæÂÂå¡å¨è°Âç¨åÂÂ为å¸Âå·æÂÂå¡å¨çÂÂæÂ¥å£ï¼Âoauth-login.cloud.huawei.com/oauth2/v3/tâ¦
POST /oauth2/v3/token HTTP/1.1
Host: oauth-login.cloud.huawei.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=<客æ·端ID>&client_secret=<客æ·端å¯ÂéÂÂ¥>
Ã¥ÂÂåºÂ示ä¾Âå¦Âä¸Âï¼Â
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
{
"access_token": "<è¿ÂÃ¥ÂÂçÂÂAccess Token>",
"expires_in": 3600,
"token_type": "Bearer"
}
- æ¨çÂÂæÂÂå¡端è°Âç¨APIæ¨éÂÂæ¶Âæ¯ï¼Â请æ±Â示ä¾Âå¦Âä¸ÂãÂÂ
HTTPS POST URLï¼Â
POST https://push-api.cloud.huawei.com/v1/clientid/messages:send
请æ±Âæ¶Âæ¯头示ä¾Âï¼Â
Content-Type: application/json; charset=UTF-8
Authorization: Bearer CF3Xl2XV6jMK************************DgAPuzvNm3WccUIaDg==
请æ±Âæ¶Âæ¯ä½Âï¼ÂéÂÂçÂ¥æ Âæ¶Âæ¯ï¼Â示ä¾Âï¼Â
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 3
}
}
},
"token": ["pushtoken1"]
}
}
èªå®Âä¹Âç¹å»æ¶Âæ¯å¨ä½Â
æ¨å¯以èªå®Âä¹Âç¹åÂȎÂÂçÂ¥æ Âæ¶Âæ¯çÂÂå¨ä½Âï¼Âä¾Âå¦Âï¼ÂæÂÂå¼ÂåºÂç¨é¦Â页ãÂÂæÂÂå¼Âç½Â页URLÃ¥ÂÂæÂÂå¼ÂåºÂç¨èªå®Âä¹Â页é¢ãÂÂ
æÂÂå¼ÂAppé¦Â页
⢠æ¨å¯以éÂÂè¿ÂAppGallery Connectæ¨éÂÂæ¶Âæ¯ï¼Â设置ç¹åÂȎÂÂçÂ¥å¨ä½Â为æÂÂå¼ÂåºÂç¨ãÂÂApp页é¢为é¦Â页ãÂÂ
⢠æ¨å¯以éÂÂè¿ÂæÂÂå¡端APIæ¨éÂÂæ¶Âæ¯ï¼Â
æ¶Âæ¯ä½Âä¸Âæº带click_actionÃ¥ÂÂ段ï¼ÂtypeÃ¥ÂÂå¼为3表示ç¹å»æ¶Âæ¯åÂÂæÂÂå¼ÂåºÂç¨é¦Â页ãÂÂ
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 3
}
}
},
"token": ["pushtoken1"]
}
}
æÂÂå¼Âç½Â页
⢠æ¨å¯以éÂÂè¿ÂAppGallery Connectæ¨éÂÂæ¶Âæ¯ï¼Â设置ç¹åÂȎÂÂçÂ¥å¨ä½Â为æÂÂå¼Âç½Â页ãÂÂ
⢠æ¨å¯以éÂÂè¿ÂæÂÂå¡端APIæ¨éÂÂæ¶Âæ¯ï¼Â
æ¶Âæ¯ä½Âä¸Âæº带click_actionÃ¥ÂÂ段ï¼ÂtypeÃ¥ÂÂå¼为2表示ç¹å»æ¶Âæ¯åÂÂæÂÂå¼Âç½Â页ãÂÂ
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 2,
"url":"https://www.huawei.com"
}
}
},
"token": ["pushtoken1"]
}
}
æÂÂå¼ÂåºÂç¨èªå®Âä¹Â页é¢
- 客æ·端åºÂç¨å ÂÃ¥ÂÂ建èªå®Âä¹Â页é¢ï¼Â以âÂÂMyActionAbilityâÂÂ为ä¾Âï¼Âï¼Âå¨æ¨项ç®çÂÂâÂÂentry/src/mainâÂÂç®å½Âä¸ÂçÂÂâÂÂconfig.jsonâÂÂæÂÂ件ä¸Âæ·»å 该AbilityçÂÂskillsÃ¥ÂÂ段ãÂÂå ¶ä¸ÂentitiesçÂÂå¼为âÂÂentity.system.defaultâÂÂä¸Âå¯æ¹åÂÂï¼ÂactionsçÂÂå¼ç±æ¨èªå®Âä¹Âï¼Â以âÂÂcom.test.myactionâÂÂ为ä¾Âï¼ÂãÂÂ
{
"orientation": "unspecified",
"name": "com.test.java.MyActionAbility",
"icon": "$media:icon",
"description": "$string:myactionability_description",
"label": "$string:entry_MyActionAbility",
"type": "page",
"launchType": "standard",
"skills": [
{
"entities": ["entity.system.default"],
"actions": ["com.test.myaction"]
}
]
}
- æ¨å¯以éÂÂè¿ÂAppGallery Connectæ¨éÂÂæ¶Âæ¯ï¼Â设置ç¹åÂȎÂÂçÂ¥å¨ä½Â为æÂÂå¼ÂåºÂç¨ãÂÂApp页é¢为èªå®Âä¹Âaction页é¢ï¼Âaction请è¾Âå ¥ä¸Âä¸ÂæÂ¥ä¸Âå®Âä¹ÂçÂÂactionsçÂÂå¼ï¼ÂãÂÂ
æ¨ä¹Âå¯以éÂÂè¿ÂæÂÂå¡端APIæ¨éÂÂæ¶Âæ¯ãÂÂæ¶Âæ¯ä½Âä¸Âæº带click_actionÃ¥ÂÂactionÃ¥ÂÂ段ï¼ÂtypeÃ¥ÂÂå¼为1表示ç¹å»æ¶Âæ¯åÂÂæÂÂå¼ÂåºÂç¨èªå®Âä¹Â页é¢ï¼ÂactionÃ¥ÂÂå¼为ä¸Âä¸ÂæÂ¥ä¸Âå®Âä¹ÂçÂÂactionsçÂÂå¼ãÂÂ
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 1,
"action":"com.test.myaction"
}
}
},
"token": ["pushtoken1"]
}
}
ä¼ éÂÂæ°æ®
æ¨æ¨éÂÂæ¶Âæ¯æ¶å¯æº带dataÃ¥ÂÂ段ï¼Âå½Âç¨æ·ç¹å»æ¶Âæ¯æ¶éÂÂè¿Âä¸Âè¿°æ¹æ³Âå°Âdataä¸ÂçÂÂæ°æ®传éÂÂè³客æ·端åºÂç¨ãÂÂ
- æ¨éÂÂæ¶Âæ¯æ¶æº带dataÃ¥ÂÂ段ï¼Â
⢠éÂÂè¿ÂAppGallery Connectæ¨éÂÂæ¶Âæ¯ä¸Â设置èªå®Âä¹Âé®å¼对ãÂÂ
⢠éÂÂè¿ÂæÂÂå¡端APIæ¨éÂÂæ¶Âæ¯ï¼Âæ¶Âæ¯ä½Âä¸Âæº带dataÃ¥ÂÂ段ãÂÂ
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 1,
"action":"com.test.myaction"
}
},
"data": "{'key_data':'value_data'}"
},
"token": ["pushtoken1"]
}
}
- å¨客æ·端åºÂç¨å¼ÂÃ¥ÂÂç¹å»æ¶Âæ¯跳转çÂÂ页é¢è·åÂÂdataÃ¥ÂÂ段çÂÂÃ¥ÂÂè½ï¼Âæ¤å¤Â以ç¹å»æ¶Âæ¯跳转å°åºÂç¨é¦Â页ï¼ÂMainAbilitySliceï¼Âè·åÂÂdataÃ¥ÂÂ段为ä¾ÂãÂÂ
public class MainAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234, "myDemo");
@Override
public void onStart(Intent intent) {
HiLog.info(LABEL_LOG, "MainAbilitySlice get started...");
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// è°Âç¨解æÂÂæ¹æ³Â
parseIntent(intent);
}
private void parseIntent(Intent intent){
if (intent == null){return;}
IntentParams intentParams = intent.getParams();
if (intentParams == null) {return;}
// è·åÂÂdataÃ¥ÂÂ段ä¸ÂçÂÂé®å¼对
String key = "key_data";
Object obj = intentParams.getParam(key);
try{
// æÂÂå°dataÃ¥ÂÂ段ä¸ÂçÂÂé®å¼对
HiLog.info(LABEL_LOG, "my key: %{public}s, my value: %{public}s", key, obj);
}catch (Exception e){
HiLog.info(LABEL_LOG, "catch exception : " + e.getMessage());
}
}
}
äºÂ解æ´å¤Â详æ >>
访é®åÂÂ为æ¨éÂÂæÂÂå¡èÂÂçÂÂå®Âç½Â
è·åÂÂÃ¥ÂÂ为æ¨éÂÂæÂÂå¡å¼ÂÃ¥ÂÂæÂÂ导æÂÂæ¡£
访é®HMS Core èÂÂçÂÂå®Âç½Â
è·åÂÂHMS Core å¼ÂÃ¥ÂÂæÂÂ导æÂÂæ¡£
堳注æÂÂ们ï¼Â第ä¸Âæ¶é´äºÂ解 HMS Core æÂÂæ°æÂÂæ¯èµÂ讯~