文档导航
Android
SDK 版本:  5.X
公告:融云新文档中心已上线,欢迎到新文档中心阅读 Android IMLibAndroid IMKit 的文档。

自定义推送通知样式

更新时间:2024-02-23 PDF

自定义推送通知样式

接收到推送消息后,系统会弹出通知,显示推送消息。

  • FCM 类型推送:针对 FCM 类型推送方式,如果在控制台设置的推送方式透传消息方式,可通过以下方式自定义通知样式。详见集成 FCM
  • 融云自建推送:可通过以下方式自定义通知样式。但因到达率较低,已不推荐使用。
  • 其他第三方类型推送:SDK 接收到其他第三方厂商的推送后,弹出的通知是系统通知,由手机系统底层直接弹出通知,所以不支持自定义。

自定义通知样式

如果您使用 FCM 推送,且 FCM 后台配置消息推送方式透传消息方式时,可通过以下方式自定义通知样式。

使用 PushEventListener

您可以设置 PushEventListener,覆写 preNotificationMessageArrived 方法,拦截通知事件并自定义显示。

提示
  • SDK 从 5.1.0 版本开始支持该功能。
  • 请在 Application 的 onCreate 方法中注册监听器。
  • 由于 Android 12 通知 trampoline 限制的原因,当您的 App 的 targetVersion >= 31 时,建议直接在 RongPushClient.setPushEventListener 中的回调中直接启动 Activity。请勿再通过广播或者服务再进行消息分发之后再启动 Activity。
RongPushClient.setPushEventListener(
                new PushEventListener() {
                    @Override
                    public boolean preNotificationMessageArrived(
                            Context context,
                            PushType pushType,
                            PushNotificationMessage notificationMessage) {
                        // 该回调仅在通知类型为透传消息时生效。返回 true 表示拦截,false 为不拦截
                        return false;
                    }

                    @Override
                    public void afterNotificationMessageArrived(
                            Context context,
                            PushType pushType,
                            PushNotificationMessage notificationMessage) {
                        // 该回调仅在通知类型为透传消息时生效
                    }

                    @Override
                    public boolean onNotificationMessageClicked(
                            Context context,
                            PushType pushType,
                            PushNotificationMessage notificationMessage) {
                        // 用户可以在此定义自己的通知的点击事件业务,返回 true 表示拦截,false 为不拦截
                        return false;
                    }

                    @Override
                    public void onThirdPartyPushState(
                            PushType pushType, String action, long resultCode) {}
                });
              
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

使用 PushMessageReceiver

您也可以通过 PushMessageReceiveronNotificationMessageArrived 方法自定义 FCM 通知样式。该方式优先级低于 PushEventListener

提示

由于 Android 12 通知 trampoline 限制的原因,当您的 App 的 targetVersion >= 31 时,建议直接在 PushMessageReceiver 的回调中直接启动 Activity。请勿再通过广播或者服务再进行消息分发之后再启动 Activity。

  1. 创建自定义 YourCustomPushMessageReceiver,并继承 PushMessageReceiver。在 onNotificationMessageArrived(通知到达事件)中可收到推送内容。

    public class YourCustomMessageReceiver extends PushMessageReceiver {
    
    @Override
    public boolean onNotificationMessageClicked(
            Context context, PushType pushType, PushNotificationMessage message) {
            if (pushType.equals(PushType.GOOGLE_FCM)){
    
            // TODO
            }
            // 返回 true 表示拦截,false 为不拦截
            return true;
    }
    }
                  
    已复制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

  2. 在主工程的 AndroidManifest.xml 中注册您自定义的推送广播接收组件。请注意替换 AndroidManifest.xml 中已添加的 PushMessageReceiver 注册内容。

      <receiver
                android:name="xxx.YourCustomPushMessageReceiver"
                android:exported="true">
                <intent-filter>
                    <action android:name="io.rong.push.intent.MESSAGE_ARRIVED" />
                    <action android:name="io.rong.push.intent.MESSAGE_CLICKED" />
                    <action android:name="io.rong.push.intent.THIRD_PARTY_PUSH_STATE" />
                </intent-filter>
            </receiver>
                  
    已复制
    1
    2
    3
    4
    5
    6
    7
    8
    9

文档反馈
意见反馈

您的改进建议

意见反馈

问题类型

联系我们

提交工单

技术支持|集成使用|产品方案


商务咨询

7 x 24 小时

为您解答方案与报价问题

131 6185 6839

文档反馈