极光推送企业开发系列之自定义消息

来源:互联网 发布:深入浅出数据分析 微盘 编辑:程序博客网 时间:2024/06/06 01:22

在企业开发中APP接收到PC后台推送的消息往往并不是最终的目的,比如商城APP中往往推送消息到APP客户端并且希望客户端能实现在使用者点击消息的同时做出相应的处理,如果给客户推送一个商品,就希望APP客户端在在用户点击通知栏的时候跳转到商品详情而不是默认跳转到APP首页,在这种使用场景下我们就需要自定义消息,要实现自定义消息我们需要做一下几件事情:

第一:了解什么是自定义消息,我们看一下官方文档

接收推送消息Receiver

支持的版本

开始的版本:最初。

功能说明

JPush SDK 收到推送,通过广播的方式,转发给开发者App,这样开发者就可以灵活地进行处理。

这个动作不是必须的。用户有需要才定义 Receiver 类来处理 SDK过来的广播。

如果不做这个动作,即不写自定义 Receiver,也不在 AndroidManifest.xml 里配置这个 Receiver,则默认的行为是:

  • 接收到推送的自定义消息,则没有被处理
  • 可以正常收到通知,用户点击打开应用主界面

接受广播

如果全部类型的广播都接收,则需要在 AndroidManifest.xml 里添加如下的配置信息:

<receiver    android:name="Your Receiver"    android:enabled="true">    <intent-filter>        <action android:name="cn.jpush.android.intent.REGISTRATION" />        <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />        <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />        <category android:name="You package Name" />    </intent-filter></receiver>

每个 Receiver action 详细解释如下。

Action - cn.jpush.android.intent.REGISTRATION

SDK 向 JPush Server 注册所得到的注册 ID 。

一般来说,可不处理此广播信息。

要深入地集成极光推送,开发者想要自己保存App用户与JPush 用户关系时,则接受此广播,取得 Registration ID 并保存与App uid 的关系到开发者自己的应用服务器上。

使用极光推送提供的别名与标签功能,是更加简单轻便的绑定App用户与JPush用户的方式,请参考文档:别名与标签使用教程。

Intent 参数
  • JPushInterface.EXTRA_REGISTRATION_ID
    • SDK 向 JPush Server 注册所得到的注册 全局唯一的 ID ,可以通过此 ID 向对应的客户端发送消息和通知。
      Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);

Action - cn.jpush.android.intent.MESSAGE_RECEIVED

收到了自定义消息 Push 。

SDK 对自定义消息,只是传递,不会有任何界面上的展示。

如果开发者想推送自定义消息 Push,则需要在 AndroidManifest.xml 里配置此 Receiver action,并且在自己写的 BroadcastReceiver 里接收处理。

Intent 参数
  • JPushInterface.EXTRA_TITLE

    • 保存服务器推送下来的消息的标题。
    • 对应 API 消息内容的 title 字段。
    • Portal 推送消息界上不作展示
      Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_TITLE);
  • JPushInterface.EXTRA_MESSAGE

    • 保存服务器推送下来的消息内容。
    • 对应 API 消息内容的 message 字段。
    • 对应 Portal 推送消息界面上的"自定义消息内容”字段。
      Bundle bundle = intent.getExtras();String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
  • JPushInterface.EXTRA_EXTRA

    • 保存服务器推送下来的附加字段。这是个 JSON 字符串。
    • 对应 API 消息内容的 extras 字段。
    • 对应 Portal 推送消息界面上的“可选设置”里的附加字段。
      Bundle bundle = intent.getExtras();String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
  • JPushInterface.EXTRA_CONTENT_TYPE

    • 保存服务器推送下来的内容类型。
    • 对应 API 消息内容的 content_type 字段。
      Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
  • JPushInterface.EXTRA_RICHPUSH_FILE_PATH

    • SDK 1.4.0 以上版本支持。
    • 富媒体通消息推送下载后的文件路径和文件名。
      Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_RICHPUSH_FILE_PATH);
  • JPushInterface.EXTRA_MSG_ID

    • SDK 1.6.1 以上版本支持。
    • 唯一标识消息的 ID, 可用于上报统计等。
      Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);

Action - cn.jpush.android.intent.NOTIFICATION_RECEIVED

收到了通知 Push。

 如果通知的内容为空,则在通知栏上不会展示通知。 但是,这个广播 Intent 还是会有。开发者可以取到通知内容外的其他信息。
Intent 参数
  • JPushInterface.EXTRA_NOTIFICATION_TITLE

    • 保存服务器推送下来的通知的标题。
    • 对应 API 通知内容的 title 字段。
    • 对应 Portal 推送通知界面上的“通知标题”字段。
      Bundle bundle = intent.getExtras();         String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
  • JPushInterface.EXTRA_ALERT

    • 保存服务器推送下来的通知内容。
    • 对应 API 通知内容的 alert 字段。
    • 对应 Portal 推送通知界面上的“通知内容”字段。
      Bundle bundle = intent.getExtras();String content = bundle.getString(JPushInterface.EXTRA_ALERT);
  • JPushInterface.EXTRA_EXTRA

    • SDK 1.2.9 以上版本支持。
    • 保存服务器推送下来的附加字段。这是个 JSON 字符串。
    • 对应 API 通知内容的 extras 字段。
    • 对应 Portal 推送消息界面上的“可选设置”里的附加字段。
      Bundle bundle = intent.getExtras();String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
  • JPushInterface.EXTRA_NOTIFICATION_ID

    • SDK 1.3.5 以上版本支持。
    • 通知栏的Notification ID,可以用于清除Notification
    • 如果服务端内容(alert)字段为空,则notification id 为0
      Bundle bundle = intent.getExtras();int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
  • JPushInterface.EXTRA_CONTENT_TYPE

    • 保存服务器推送下来的内容类型。
    • 对应 API 消息内容的 content_type 字段。
    • Portal 上暂时未提供输入字段。
      Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
  • JPushInterface.EXTRA_RICHPUSH_HTML_PATH

    • SDK 1.4.0 以上版本支持。
    • 富媒体通知推送下载的HTML的文件路径,用于展现WebView。
      Bundle bundle = intent.getExtras();String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);
  • JPushInterface.EXTRA_RICHPUSH_HTML_RES

    • SDK 1.4.0 以上版本支持。
    • 富媒体通知推送下载的图片资源的文件名,多个文件名用 “,” 分开。 与 “JPushInterface.EXTRA_RICHPUSH_HTML_PATH” 位于同一个路径。
      Bundle bundle = intent.getExtras();String fileStr = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_RES);String[] fileNames = fileStr.split(",");
  • JPushInterface.EXTRA_MSG_ID

    • SDK 1.6.1 以上版本支持。
    • 唯一标识通知消息的 ID, 可用于上报统计等。
      Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);

Action - cn.jpush.android.intent.NOTIFICATION_OPENED

用户点击了通知。

一般情况下,用户不需要配置此 receiver action。

如果开发者在 AndroidManifest.xml 里未配置此 receiver action,那么,SDK 会默认打开应用程序的主 Activity,相当于用户点击桌面图标的效果。

如果开发者在 AndroidManifest.xml 里配置了此 receiver action,那么,当用户点击通知时,SDK 不会做动作。开发者应该在自己写的 BroadcastReceiver 类里处理,比如打开某 Activity 。

Intent 参数
  • JPushInterface.EXTRA_NOTIFICATION_TITLE

    • 保存服务器推送下来的通知的标题。
    • 对应 API 通知内容的 title 字段。
    • 对应 Portal 推送通知界面上的“通知标题”字段。
      Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
  • JPushInterface.EXTRA_ALERT

    • 保存服务器推送下来的通知内容。
    • 对应 API 通知内容的alert字段。
    • 对应 Portal 推送通知界面上的“通知内容”字段。
      Bundle bundle = intent.getExtras();String content = bundle.getString(JPushInterface.EXTRA_ALERT);
  • JPushInterface.EXTRA_EXTRA

    • SDK 1.2.9 以上版本支持。
    • 保存服务器推送下来的附加字段。这是个 JSON 字符串。
    • 对应 API 消息内容的 extras 字段。
    • 对应 Portal 推送消息界面上的“可选设置”里的附加字段。
      Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
  • JPushInterface.EXTRA_NOTIFICATION_ID

    • SDK 1.3.5 以上版本支持。
    • 通知栏的Notification ID,可以用于清除Notification
      Bundle bundle = intent.getExtras();int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID
  • JPushInterface.EXTRA_MSG_ID

    • SDK 1.6.1 以上版本支持。
    • 唯一标识调整消息的 ID, 可用于上报统计等。
      Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);

代码示例

public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        Log.d(TAG, "onReceive - " + intent.getAction());        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {            System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));            // 自定义消息不会展示在通知栏,完全要开发者写代码去处理        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            System.out.println("收到了通知");            // 在这里可以做些统计,或者做些其他工作        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {            System.out.println("用户点击打开了通知");            // 在这里可以自己写代码去定义用户点击后的行为            Intent i = new Intent(context, TestActivity.class);  //自定义打开的界面            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            context.startActivity(i);        } else {            Log.d(TAG, "Unhandled intent - " + intent.getAction());  }}

大家是否看到官方文档一大串那么长心里特别不乐意呢,其实文档说的我们无需记住,我们只要知道出了问题去哪里查找就好了,文档说明了如果像自定义消息的跳转链接就必须自定义一个Receiver,然后根据public void onReceive(Context context, Intent intent)中intent.getAction()来判断返回值的意图,比如当JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())时极光服务器是要告诉我们用户点击了通知,我们可以在此判断中进行页面跳转

问题:在企业开发中我们是如何进行开发自定义消息的呢?

答:

第一:一般在企业开发中此时后台会和APP开发人员进行接口约定,约定什么内容呢?想想就能明白,我需要后台告诉我,用户点击通知栏时此时要往APP的那个页面进行跳转类似:


第二:我们只需要根据拿到的约定好的条件,在用户点开了通知的时候进行相对应的跳转

public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        Log.d(TAG, "onReceive - " + intent.getAction());        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {            System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));            // 自定义消息不会展示在通知栏,完全要开发者写代码去处理        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            System.out.println("收到了通知");            // 在这里可以做些统计,或者做些其他工作        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {            System.out.println("用户点击打开了通知");            // 在这里可以自己写代码去定义用户点击后的行为            Intent i = new Intent(context, TestActivity.class);  //自定义打开的界面            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            context.startActivity(i);        } else {            Log.d(TAG, "Unhandled intent - " + intent.getAction());  }}
经过以上简单的步骤就能完成自定义消息的跳转
0 0