admin管理员组

文章数量:1530279

首先参考地址:文档中心

2、打开当前app内任意一个Activity

调用Message.Builder类的extra(String key, String value)方法:

key - Constants.EXTRA_PARAM_NOTIFY_EFFECT, value - Constants.NOTIFY_ACTIVITY

key - Constants.EXTRA_PARAM_INTENT_URI, value - 启动Activity的intent uri

获取Intent uri的具体步骤:

在安卓客户端的工程的AndroidManifest.xml里定义一个Activity, 例如下面是一个展示新闻的Activity:

<activity android:name=".NewsActivity"/>

定义一个Intent来启动该Activity, 例如要打开上面定义的NewsActivity可定义Intent为:

intent:#Intent;component=com.yourpackage/.YourActivity;end

例如转换intent为uri:

String uriString = intent.toUri(Intent.URI_INTENT_SCHEME);//该uriString就是Constants.EXTRA_PARAM_INTENT_URI对应的值

返回值:

intent:#Intent;component=com.xiaomi.mipushdemo/.NewsActivity;end

注:

  • 如果你希望在Intent uri中携带数据,注意一个限制:Intent.toUri(Intent.URI_INTENT_SCHEME), 返回的结果不会携带除基本类型(boolean, byte, short, int, long, float, double, String)以外的数据类型, 例如数组和HashMap
  • 获取了Intent uri之后, 在服务端的工程里(需要引用推送服务器SDK), 把uriString 设置进的Message.Builder.extra(),对应的key是Constants.EXTRA_PARAM_INTENT_URI
  • 在生成intent uri时,如果出现点击无法跳转或者Activity的生命周期没有执行时,建议追加flag参数:intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

Demo:

private Message buildMessage() throws Exception {
    String PACKAGENAME = "com.xiaomi.mipushdemo";
    String messagePayload = “This is a message”;
    String title = “notification title”;
    String description = “notification description”;
    return new Message.Builder()
            .title(title)
            .description(description).payload(messagePayload)
            .restrictedPackageName(MY_PACKAGE_NAME)
            .passThrough(0)  //消息使用通知栏方式
            .notifyType(1)
            .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_ACTIVITY)
            .extra(Constants.EXTRA_PARAM_INTENT_URI, "intent:#Intent;component=com.xiaomi.mipushdemo/.NewsActivity;end")
            .build();
}

3、打开网页

调用Message.Builder类的extra(String key, String value)方法:

key -Constants.EXTRA_PARAM_NOTIFY_EFFECT, value - Constants.NOTIFY_WEB

key - Constants.EXTRA_PARAM_WEB_URI, value - 网页uri

Demo:

private Message buildMessage() throws Exception {
    String PACKAGENAME = "com.xiaomi.mipushdemo";
    String messagePayload = “This is a message”;
    String title = “notification title”;
    String description = “notification description”;
    Message message = new Message.Builder()
            .title(title)
            .description(description).payload(messagePayload)
            .restrictedPackageName(MY_PACKAGE_NAME)
            .passThrough(0)  //消息使用通知栏方式
            .notifyType(1)
            .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_WEB)
            .extra(Constants.EXTRA_PARAM_WEB_URI, "http://www.xiaomi")
            .build();
    return message;
}

本文标签: 小米拉起消息通知android