zhangxaochen
zhangxaochen

Reputation: 34047

My phone cannot receive any message after calling the HMS Push Kit server API. The SDK version is 4.0.3.300

I'm using Huawei's Push Kit to push messages to Huawei devices, and the SDK version is 4.0.3.300. I can obtain the token correctly, so I start to use Postman to call the Push Kit server API for pushing messages. The returned message body shows success, but my device didn't receive any message. The following are the code and my operations. What shall I do to handle this problem? Postman call address:

POST https://push-api.cloud.huawei.com/v1/[myappid]/messages:send

Message body sent:

{
    "validate_only": true,
    "message": {
        "data": "my data",
        "android": {
            "fast_app_target": 2
        },
        "token": [
            "my token"
        ]
    }
}

Response:

{
    "code": "80000000",
    "msg": "Success",
    "requestId": "158929224618234594000607"
}

The response indicates success, but my device did not receive any data message.

I've already inherited HmsMessageService and configure it in the manifest file.

public class MyPushService extends HmsMessageService {
private static final String TAG = "hmspush";

@Override
public void onMessageReceived(RemoteMessage message) {
Log.i(TAG, "onMessageReceived is called");
if (message == null) {
Log.e(TAG, "Received message entity is null!");
return;
}
Log.i(TAG, "getCollapseKey: " + message.getCollapseKey()
+ "\n getData: " + message.getData()
+ "\n getFrom: " + message.getFrom()
+ "\n getTo: " + message.getTo()
+ "\n getMessageId: " + message.getMessageId()
+ "\n getSendTime: " + message.getSentTime()
+ "\n getMessageType: " + message.getMessageType()
+ "\n getTtl: " + message.getTtl());
}
}
<service
android:name=".MyPushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>

Upvotes: 1

Views: 930

Answers (1)

zhangxaochen
zhangxaochen

Reputation: 34047

The problem is caused by the incorrect message body. In the message body, there is a field named validate_only. According to the description on HUAWEI Developers, this field determines whether a message is a test message. A test message requires only format verification and will not be pushed to the user device. Value true indicates a test message; value false indicates a normal message. Because the value of the validate_only field is true in the given scenario, the message is a test message and therefore cannot be received by the device. To solve the problem, change the value of validate_only to false:

{
    "validate_only": false,
    "message": {
        "data": "my data",
        "android": {
            "fast_app_target": 2
        },
        "token": [
            "my token"
        ]
    }
}

Upvotes: 4

Related Questions