android - 如何访问HMS推送通知的payload?

标签 android push-notification huawei-mobile-services android-push-notification

我已经在华为 AppGallery 上发布了一个 Android 应用,并且能够通过 HMS Push Service 从我的应用后端服务器发送推送通知到手机,如下所述。

但是,我想知道 如何访问推送通知负载 在应用程序中:

screenshot

这是我目前发送推送通知的方式 -

首先,我的后端 POST 到 https://login.vmall.com/oauth2/token以下:

grant_type=client_credentials&
client_id=MY_APP_ID&
client_secret=MY_APP_SECRET

并成功从 HMS 后端获取访问 token :
{
"access_token":"CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==",
"expires_in":604800
}

然后我的后端 POST 到(即 {"ver":"1", "appId":"MY_APP_ID"} 的 URL 编码) -
https://api.push.hicloud.com/pushsend.do?nsp_ctx=
    %7B%22ver%22%3A%221%22%2C+%22appId%22%3A%22101130655%22%7D

以下 URL 编码的正文:
access_token=CF1/tI97Ncjts68jeXaUmxjmu8BARYGCzd2UjckO5SphMcFN/EESRlfPqhi37EL7hI2YQgPibPpE7xeCI5ej/A==
&nsp_svc=openpush.message.api.send
&nsp_ts=1568056994
&device_token_list=%5B%220869268048295821300004507000DE01%22%5D
&payload=%7B%22hps%22%3A%7B%22msg%22%3A%7B%22action%22%3A%7B%22param%22%3A%7B%22appPkgName%22%3A%22de%2Eslova%2Ehuawei%22%7D%2C%22type%22%3A3%7D%2C%22type%22%3A3%2C%22body%22%3A%7B%22title%22%3A%22Alexander%3A+How+to+access+payload%3F%22%2C%22content%22%3A%22Alexander%3A+How+to+access+payload%3F%22%7D%7D%2C%22ext%22%3A%7B%22gid%22%3A86932%7D%7D%7D
payload值是(我不确定它是否具有正确的 JSON 结构以及“类型”3 的真正含义):
{
  "hps": {
    "msg": {
      "action": {
        "param": {
          "appPkgName": "de.slova.huawei"
        },
        "type": 3
      },
      "type": 3,
      "body": {
        "title": "Alexander:+How+to+access+payload?",
        "content": "Alexander:+How+to+access+payload?"
      }
    },
    "ext": {
      "gid": 86932
    }
  }
}

我需要提取自定义整数“gid”值(我的应用程序中的“游戏 id”)。

在自定义接收器类中,我定义了以下方法,但没有调用它们(除了 onToken 方法 - 当我的应用程序启动并通过调用 HuaweiPush.HuaweiPushApi.getToken 方法从 HMS 异步请求“推送 token ”时):
public class MyReceiver extends PushReceiver {
    private final static String BELONG_ID =  "belongId";

    @Override
    public void onToken(Context context, String token, Bundle extras) {
        String belongId = extras.getString(BELONG_ID);
        Log.d(TAG, "onToken belongId=" + belongId + ", token=" + token);
    }

    // this method is called for transparent push messages only NOT CALLED
    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        String content = new String(msg, "UTF-8");
        Log.d(TAG, "onPushMsg content=" + content);
        return true;
    }

    // this method is when a notification bar message is clicked NOT CALLED
    @Override
    public void onEvent(Context context, Event event, Bundle extras) {
        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
            int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
            Log.d(TAG, "onEvent notifyId=" + notifyId);
            if (notifyId != 0) {
                NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notifyId);
            }
        }

        String msg = extras.getString(BOUND_KEY.pushMsgKey);
        Log.d(TAG, "onEvent msg=" + msg);
        super.onEvent(context, event, extras);
    }

    // this method is called when push messages state changes
    @Override
    public void onPushState(Context context, boolean pushState) {
        Log.d(TAG, "onPushState pushState=" + pushState);
    }
}

请帮助我通过 HMS 推送通知将自定义整数值从我的后端传递到应用程序。

最佳答案

感谢华为开发人员提供的帮助(在向他们发送 adb shell setprop log.tag.hwpush VERBOSE 日志后),现在一切都已解决 -
AndroidManifest.xml 中,我添加了一个自定义方案 app (可以是任何字符串),如 Google doc Create Deep Links to App Content 中所述:

<activity android:name="de.slova.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="app" android:host="slova.de" />
    </intent-filter>
</activity>
MainActivity.java 我添加了用于解析 Intent 的代码:
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // getIntent() should always return the most recent intent
    handleIntent(intent);
}

private boolean handleIntent(Intent intent) {
    try {
        String gidStr = intent.hasExtra("gid") ?
                intent.getStringExtra("gid") :             // FCM notification
                intent.getData().getQueryParameter("gid"); // HMS notification
        Log.d(TAG, "handleIntent gidStr=" + gidStr);
        int gid = Integer.parseInt(gidStr);
        // show the game when user has tapped a push notification
        showGame(gid);
        return true;
    } catch (Exception ex) {
        Log.w(TAG, "handleIntent", ex);
    }

    return false;
}
顺便说一句,我在应用程序启动时清除推送通知:
@Override
public void onResume() {
    super.onResume();

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancelAll();
}
最后在我的游戏后端,我首先通过 POST 获取 token
https://login.cloud.huawei.com/oauth2/v2/token
grant_type=client_credentials&
client_secret=MY_APP_SECRET&
client_id=MY_APP_ID
然后使用 header Authorization: Bearer MY_TOKENContent-Type: application/json; charset=UTF-8 发布通知 https://push-api.cloud.huawei.com/v1/MY_APP_ID/messages:send 。 JSON 格式在 HMS PushKit 文档中进行了描述。
{ 
   "message": { 
      "android": { 
         "notification": { 
            "image": "https://slova.de/ws/board3?gid=108250",
            "title": "Game 108250",
            "body": "Alexander: Test chat msg",
            "click_action": { 
               "type": 1,
               "intent": "app://slova.de/?gid=108250"
            }
         }
      },
      "token": [ 
         "1234567890123456789000000000DE01"
      ]
   }
}
我目前在我的 build.gradle 中使用的HMS SDK目前有:
implementation "com.huawei.hms:base:3.0.0.301"
implementation "com.huawei.hms:hwid:3.0.0.301"
implementation "com.huawei.hms:push:3.0.0.301"
implementation "com.huawei.hms:iap:3.0.0.301"
这适用于 my word game :推送通知到达,带有标题、正文和小图像。然后游戏#108250 由我的应用程序打开,当用户点击它时:
app screenshot

关于android - 如何访问HMS推送通知的payload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860791/

相关文章:

java - android.app.RemoteServiceException : Bad notification posted on huawei Y6

android - 服务和 Activity 之间的通信

ios - "UserNotificationsUI"在构建 UIKit for Mac 错误时不可用

android - 华为IAP 当选择中国大陆时 : MAJOR:22: Integrate the version update API (checkUpdate)

android - Pushwoosh 和 Android Phonegap 应用程序无法通信

android - GCMBaseIntentService 回调仅在根包中

ionic-framework - Flurry Analytics 是否支持没有 Playstore/GMS 服务的华为设备上的标志性应用程序?

java - 检查参数中的类是否等于(instanceOf)所需的类。尝试创建通用类

android - ActionBarSherlock 开关切换

android - 为用户提供播放带或不带音频的视频的选项