[Huawei Developers] John Wo:
Hi Folks :)

I will introduce you to how to implement Push Kit both Android side and server side. Also I will show you how to send push notification from backend, catch Android side and open specific page on Android app.

Backend Development

...
SEND_PUSH_REQUEST_URL = https://push-api.cloud.huawei.com/v1/APP_ID/messages:send
ACCESS_TOKEN_CONTENT = grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET
ACCESS_TOKEN_REQUEST_URL = https://oauth-login.cloud.huawei.com/oauth2/v2/token?grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET
...

There is application.properties class on backend project. There are using to get access token and send notification process. You must get APP_ID and APP_SECRET ‘s values from AGC.

Preparing JSON Object For Notification

public class PushNotification {
private final Message message;
}
public class Message {
private Android android;
private List<String> token;
}
public class Android {
private int collapse_key;
private String urgency;
private Notification notification;
}
public class Notification {
private String title;
private String body;
private Click_Action click_action;
}
public class Click_Action {
private int type;
private String intent;
}

The classes for create required JSON object on the below.

public class PushNotificationProcess {
private static final String AUTH = "Authorization";
private static final String METHOD = "POST";
private static final String ACCESS_TOKEN_CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String CONTENT_TYPE_KEY = "Content-Type";
private static final String SEND_PUSH_CONTENT_TYPE = "application/json";
@Autowired
private Environment environment;
private String accessToken = "";
public void getAccessToken() {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse(ACCESS_TOKEN_CONTENT_TYPE);
RequestBody body = RequestBody.create(mediaType, environment.getProperty("ACCESS_TOKEN_CONTENT"));
Request request = new Request.Builder()
.url(environment.getProperty("ACCESS_TOKEN_REQUEST_URL"))
.method(METHOD, body)
.addHeader(CONTENT_TYPE_KEY, ACCESS_TOKEN_CONTENT_TYPE)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if (response.isSuccessful()) {
ObjectMapper objectMapper = new ObjectMapper();
ResponseBody responseBody = client.newCall(request).execute().body();
AccessToken entity = objectMapper.readValue(responseBody.string(), AccessToken.class);
accessToken = entity.getToken_type() + " " + entity.getAccess_token();
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendPushNotification(String pushBody) {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse(SEND_PUSH_CONTENT_TYPE);
RequestBody body = RequestBody
.create(mediaType, pushBody);
Request request = new Request.Builder()
.url(environment.getProperty("SEND_PUSH_REQUEST_URL"))
.method(METHOD, body)
.addHeader(AUTH, accessToken)
.addHeader(CONTENT_TYPE_KEY, SEND_PUSH_CONTENT_TYPE)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try {
if (response.isSuccessful()) {
//Success
} else {
//Handler error code
System.out.println(response.code());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
System.out.println("onFailure");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}

There are 2 functions on the PushNotificationProcess class. The functions about HTTP Request to Huawei Push Kit server. I used OkHttpClient library also you can use Retrofit or something like that. I don’t explain static strings ’cause I think they are clear.

When the calling onResponse method on getAccessToken function, firstly I check response.isSuccesful and then I can get the response body. We must use the access_token like “Baerer xxxx” so I combine token_type and token on the 30. line and the access_token ready to use.

sendPushNotification looks like getAccessToken. I mean there is another HTTP Request. The method has a string param as pushBody -I will explain next part- . I get some strings from application.properties class, you can see 51. line.

public class PushNotification {
...
private void prepareNotification() {
this.message.setToken(new ArrayList<>());
this.message.getAndroid().setCollapse_key(0);
this.message.getAndroid().setUrgency("NORMAL");
}
public void prepareIntent(int topicId) {
this.message.getAndroid().getNotification().getClick_action().setType(1);
this.message.getAndroid().getNotification().getClick_action()
.setIntent(Constant.DataMessage.SCHEME_URL + Constant.DataMessage.SCHEME_URL_FORUM + topicId + Constant.DataMessage.SCHEME_URL_END);
}
public void prepareContent(String title, String body) {
this.message.getAndroid().getNotification().setTitle(title);
this.message.getAndroid().getNotification().setBody(body);
}
public void prepareTokens(List<String> tokenList) {
this.message.getToken().addAll(tokenList);
}
public String getBodyAsString() {
return new Gson().toJson(this);
}
}

More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0203442155932950046