logo文档
搜索

本地通知消息

低级通知

  • 没有铃声/震动/led灯
  • 仅在通知栏展示通知消息

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("low_priority_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a low priority notification") .setPriority(Notification.PRIORITY_LOW); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("low_priority_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a low priority notification")
                .setPriority(Notification.PRIORITY_LOW);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("low_priority_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a low priority notification") .setPriority(Notification.PRIORITY_LOW) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("low_priority_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a low priority notification")
                .setPriority(Notification.PRIORITY_LOW)
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

普通通知

  • 有铃声/震动/led灯,可通过defaultProperties配置铃声/震动/led灯
  • 在通知栏展示通知消息
  • 在状态栏展示通知小图标

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("normal_priority_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a normal priority notification") // 这里演示仅铃声和震动生效 .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setPriority(Notification.PRIORITY_DEFAULT); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("normal_priority_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a normal priority notification")
                // 这里演示仅铃声和震动生效
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_DEFAULT);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("normal_priority_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a normal priority notification") // 这里演示仅铃声和震动生效 .setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE) .setPriority(Notification.PRIORITY_DEFAULT) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("normal_priority_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a normal priority notification")
                // 这里演示仅铃声和震动生效
                .setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_DEFAULT)
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

高级通知

  • 有铃声/震动/led灯,可通过defaultProperties配置铃声/震动/led灯
  • 在通知栏展示通知消息
  • 在状态栏展示通知小图标
  • 设置应用允许悬浮窗后,可在屏幕顶部悬浮窗弹出

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("high_priority_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a high priority notification") // 这里演示铃声/震动/led灯全部生效 .setDefaults(Notification.DEFAULT_ALL) .setPriority(Notification.PRIORITY_HIGH); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("high_priority_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a high priority notification")
                // 这里演示铃声/震动/led灯全部生效
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_HIGH);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("high_priority_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a high priority notification") // 这里演示铃声/震动/led灯全部生效 .setDefaults(Notification.DEFAULT_ALL) .setPriority(Notification.PRIORITY_HIGH) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("high_priority_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a high priority notification")
                // 这里演示铃声/震动/led灯全部生效
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_HIGH)
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

大文本风格通知

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("big_text_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a big text notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT) .setBigText("《再别康桥》是现代诗人徐志摩脍炙人口的诗篇,是新月派诗歌的代表作品。全诗以离别康桥时感情起伏为线索,抒发了对康桥依依惜别的深情。语言轻盈柔和,形式精巧圆熟,诗人用虚实相间的手法,描绘了一幅幅流动的画面,构成了一处处美妙的意境,细致入微地将诗人对康桥的爱恋,对往昔生活的怀念,对眼前的无可奈何的离愁,表现得真挚、浓郁、隽永,是徐志摩诗作中的绝唱。"); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("big_text_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a big text notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
                .setBigText("《再别康桥》是现代诗人徐志摩脍炙人口的诗篇,是新月派诗歌的代表作品。全诗以离别康桥时感情起伏为线索,抒发了对康桥依依惜别的深情。语言轻盈柔和,形式精巧圆熟,诗人用虚实相间的手法,描绘了一幅幅流动的画面,构成了一处处美妙的意境,细致入微地将诗人对康桥的爱恋,对往昔生活的怀念,对眼前的无可奈何的离愁,表现得真挚、浓郁、隽永,是徐志摩诗作中的绝唱。");
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("big_text_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a big text notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT) .setBigText("《再别康桥》是现代诗人徐志摩脍炙人口的诗篇,是新月派诗歌的代表作品。全诗以离别康桥时感情起伏为线索,抒发了对康桥依依惜别的深情。语言轻盈柔和,形式精巧圆熟,诗人用虚实相间的手法,描绘了一幅幅流动的画面,构成了一处处美妙的意境,细致入微地将诗人对康桥的爱恋,对往昔生活的怀念,对眼前的无可奈何的离愁,表现得真挚、浓郁、隽永,是徐志摩诗作中的绝唱。") MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("big_text_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a big text notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
                .setBigText("《再别康桥》是现代诗人徐志摩脍炙人口的诗篇,是新月派诗歌的代表作品。全诗以离别康桥时感情起伏为线索,抒发了对康桥依依惜别的深情。语言轻盈柔和,形式精巧圆熟,诗人用虚实相间的手法,描绘了一幅幅流动的画面,构成了一处处美妙的意境,细致入微地将诗人对康桥的爱恋,对往昔生活的怀念,对眼前的无可奈何的离愁,表现得真挚、浓郁、隽永,是徐志摩诗作中的绝唱。")
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

收件箱风格通知

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("inbox_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a inbox notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_INBOX) .setInbox(new String[]{"this is inbox one", "this is inbox two", "this is inbox three"}); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("inbox_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a inbox notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_INBOX)
                .setInbox(new String[]{"this is inbox one", "this is inbox two", "this is inbox three"});
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("inbox_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a inbox notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_INBOX) .setInbox(arrayOf("this is inbox one", "this is inbox two", "this is inbox three")) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("inbox_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a inbox notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_INBOX)
                .setInbox(arrayOf("this is inbox one", "this is inbox two", "this is inbox three"))
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

大图片风格通知

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("big_picture_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a big picture notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_PICTURE) .setBigPicture( "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=96071541,1913562332&fm=26&gp=0.jpg"); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("big_picture_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a big picture notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_PICTURE)
                .setBigPicture( "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=96071541,1913562332&fm=26&gp=0.jpg");
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("big_picture_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a big picture notification") .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_PICTURE) .setBigPicture("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=96071541,1913562332&fm=26&gp=0.jpg") MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("big_picture_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a big picture notification")
                .setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_PICTURE)
                .setBigPicture("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=96071541,1913562332&fm=26&gp=0.jpg")
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

自定义角标通知

  • 仅支持华为/荣耀/小米
  • 小米设备上,删除通知,角标数量自动减1
  • 华为/荣耀设备上,删除通知,角标数量不会自动减1,需要调用MTPushPrivatesApi.setNotificationBadge(context,number)设置最终角标数量

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("badge_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a badge notification") // 累加逻辑,这里演示角标数量+1 .setBadge(1); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("badge_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a badge notification")
                // 累加逻辑,这里演示角标数量+1
                .setBadge(1);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("badge_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a badge notification") // 累加逻辑,这里演示角标数量+1 .setBadge(1) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("badge_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a badge notification")
                // 累加逻辑,这里演示角标数量+1
                .setBadge(1)
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

自定义铃声通知

  • 需要提前在res/raw/下放好对应铃声,这里以"coin.mp3"举例
  • Android8.0开始,铃声在channel中设置

Java:

NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("sound_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a sound notification") .setSound("coin"); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("sound_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a sound notification")
                .setSound("coin");
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

val notificationMessage = NotificationMessage() .setMessageId("sound_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a sound notification") .setSound("coin") MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      val notificationMessage = NotificationMessage()
                .setMessageId("sound_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a sound notification")
                .setSound("coin")
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

自定义channel通知

Java:

NotificationChannel notificationChannel = buildChannel(); NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("channel_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a channel notification") .setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId()); MTPushPrivatesApi.showNotification(this, notificationMessage); // 演示如何创建一个channel,普通级别,附带铃声coin private NotificationChannel buildChannel() { try { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { return null; } NotificationManager notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager == null) { return null; } String sound = "coin"; // channel id String channelId = "money"; // channel 名 String channelName = "金钱"; // channel 优先级 int importance = NotificationManager.IMPORTANCE_DEFAULT; // channel 描述 String channelDescription = "用于演示channel,带自定义铃声"; NotificationChannel channel = notificationManager.getNotificationChannel(channelId); if (channel == null) { channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(channelDescription); channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/raw/" + sound); channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(channel); } else { Log.d(TAG, "channel: [" + channelId + "] is already exists"); } return channel; } catch (Throwable throwable) { throwable.printStackTrace(); } return null; }
              
                      NotificationChannel notificationChannel = buildChannel();
        NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("channel_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a channel notification")
                .setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId());
        MTPushPrivatesApi.showNotification(this, notificationMessage);


    // 演示如何创建一个channel,普通级别,附带铃声coin
    private NotificationChannel buildChannel() {
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                return null;
            }
            NotificationManager notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager == null) {
                return null;
            }
            String sound = "coin";
            // channel id
            String channelId = "money";
            // channel 名
            String channelName = "金钱";
            // channel 优先级
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            // channel 描述
            String channelDescription = "用于演示channel,带自定义铃声";
            NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
            if (channel == null) {
                channel = new NotificationChannel(channelId, channelName, importance);
                channel.setDescription(channelDescription);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/raw/" + sound);
                channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            } else {
                Log.d(TAG, "channel: [" + channelId + "] is already exists");
            }
            return channel;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

            
此代码块在浮窗中显示

Kotlin:

val notificationChannel = buildChannel() val notificationMessage = NotificationMessage() .setMessageId("channel_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a channel notification") .setChannelId(if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) "" else notificationChannel?.id ?: "") MTPushPrivatesApi.showNotification(this, notificationMessage) // 演示如何创建一个channel,普通级别,附带铃声coin private fun buildChannel(): NotificationChannel? { return try { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { return null } val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager if (notificationManager == null) { return null } val sound = "coin" // channel id val channelId = "money" // channel 名 val channelName = "金钱" // channel 优先级 val importance = NotificationManager.IMPORTANCE_DEFAULT // channel 描述 val channelDescription = "用于演示channel,带自定义铃声" var channel = notificationManager.getNotificationChannel(channelId) if (channel == null) { channel = NotificationChannel(channelId, channelName, importance) channel.description = channelDescription channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE val soundUri = Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${packageName}/raw/$sound") channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT) notificationManager.createNotificationChannel(channel) } else { Log.d(TAG, "channel: [$channelId] is already exists") } channel } catch (throwable: Throwable) { throwable.printStackTrace() null } }
              
                      val notificationChannel = buildChannel()
        val notificationMessage = NotificationMessage()
                .setMessageId("channel_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a channel notification")
                .setChannelId(if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) "" else notificationChannel?.id ?: "")
        MTPushPrivatesApi.showNotification(this, notificationMessage)


    // 演示如何创建一个channel,普通级别,附带铃声coin
    private fun buildChannel(): NotificationChannel? {
        return try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                return null
            }
            val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
            if (notificationManager == null) {
                return null
            }
            val sound = "coin"
            // channel id
            val channelId = "money"
            // channel 名
            val channelName = "金钱"
            // channel 优先级
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            // channel 描述
            val channelDescription = "用于演示channel,带自定义铃声"
            var channel = notificationManager.getNotificationChannel(channelId)
            if (channel == null) {
                channel = NotificationChannel(channelId, channelName, importance)
                channel.description = channelDescription
                channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
                val soundUri = Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${packageName}/raw/$sound")
                channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT)
                notificationManager.createNotificationChannel(channel)
            } else {
                Log.d(TAG, "channel: [$channelId] is already exists")
            }
            channel
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
            null
        }
    }

            
此代码块在浮窗中显示

自定义布局通知

Java:

// 这里定义一个常量,代表自定义布局的builderId private static final int BUILDER_ID = 1001; // 首先设置自定义布局到Engagelab SDK NotificationLayout customBuilder = new NotificationLayout() .setLayoutId(R.layout.custom_notification_layout) .setIconViewId(R.id.iv_notification_icon) .setIconResourceId(R.drawable.mtpush_notification_icon) .setTitleViewId(R.id.tv_notification_title) .setContentViewId(R.id.tv_notification_content) .setTimeViewId(R.id.tv_notification_time); MTPushPrivatesApi.setNotificationLayout(this.getApplicationContext(), BUILDER_ID, customBuilder); // 之后发送自定义布局通知 NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("custom_layout_" + System.currentTimeMillis()) .setBuilderId(BUILDER_ID) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a custom layout notification"); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      // 这里定义一个常量,代表自定义布局的builderId
        private static final int BUILDER_ID = 1001;
        // 首先设置自定义布局到Engagelab SDK
        NotificationLayout customBuilder = new NotificationLayout()
                .setLayoutId(R.layout.custom_notification_layout)
                .setIconViewId(R.id.iv_notification_icon)
                .setIconResourceId(R.drawable.mtpush_notification_icon)
                .setTitleViewId(R.id.tv_notification_title)
                .setContentViewId(R.id.tv_notification_content)
                .setTimeViewId(R.id.tv_notification_time);
        MTPushPrivatesApi.setNotificationLayout(this.getApplicationContext(), BUILDER_ID, customBuilder);
        // 之后发送自定义布局通知
        NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("custom_layout_" + System.currentTimeMillis())
                .setBuilderId(BUILDER_ID)
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a custom layout notification");
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

// 这里定义一个常量,代表自定义布局的builderId companion object { private const val BUILDER_ID = 1001 } // 首先设置自定义布局到Engagelab SDK val customBuilder = NotificationLayout() .setLayoutId(R.layout.custom_notification_layout) .setIconViewId(R.id.iv_notification_icon) .setIconResourceId(R.drawable.mtpush_notification_icon) .setTitleViewId(R.id.tv_notification_title) .setContentViewId(R.id.tv_notification_content) .setTimeViewId(R.id.tv_notification_time) MTPushPrivatesApi.setNotificationLayout(applicationContext, BUILDER_ID, customBuilder) // 之后发送自定义布局通知 val notificationMessage = NotificationMessage() .setMessageId("custom_layout_${System.currentTimeMillis()}") .setBuilderId(BUILDER_ID) .setTitle(applicationContext.packageName) .setContent("this is a custom layout notification") MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      // 这里定义一个常量,代表自定义布局的builderId
        companion object {
            private const val BUILDER_ID = 1001
        }
        // 首先设置自定义布局到Engagelab SDK
        val customBuilder = NotificationLayout()
                .setLayoutId(R.layout.custom_notification_layout)
                .setIconViewId(R.id.iv_notification_icon)
                .setIconResourceId(R.drawable.mtpush_notification_icon)
                .setTitleViewId(R.id.tv_notification_title)
                .setContentViewId(R.id.tv_notification_content)
                .setTimeViewId(R.id.tv_notification_time)
        MTPushPrivatesApi.setNotificationLayout(applicationContext, BUILDER_ID, customBuilder)
        // 之后发送自定义布局通知
        val notificationMessage = NotificationMessage()
                .setMessageId("custom_layout_${System.currentTimeMillis()}")
                .setBuilderId(BUILDER_ID)
                .setTitle(applicationContext.packageName)
                .setContent("this is a custom layout notification")
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

自定义跳转通知

Java:

// 首先获取intentUri Intent intent = new Intent(); intent.setClassName(this.getPackageName(), IntentActivity.class.getCanonicalName()); intent.setAction(ExampleGlobal.ACTION_INTENT_NOTIFICATION); Bundle bundle = new Bundle(); bundle.putString("description", "this is a intent notification"); bundle.putString("form", MainActivity.class.getSimpleName()); bundle.putString("to", IntentActivity.class.getSimpleName()); intent.putExtras(bundle); String intentUri = intent.toURI(); // 之后发送自定义跳转通知 NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("intent_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a intent notification") .setTargetEvent(intentUri); MTPushPrivatesApi.showNotification(this, notificationMessage);
              
                      // 首先获取intentUri
        Intent intent = new Intent();
        intent.setClassName(this.getPackageName(), IntentActivity.class.getCanonicalName());
        intent.setAction(ExampleGlobal.ACTION_INTENT_NOTIFICATION);
        Bundle bundle = new Bundle();
        bundle.putString("description", "this is a intent notification");
        bundle.putString("form", MainActivity.class.getSimpleName());
        bundle.putString("to", IntentActivity.class.getSimpleName());
        intent.putExtras(bundle);
        String intentUri = intent.toURI();
                // 之后发送自定义跳转通知
        NotificationMessage notificationMessage = new NotificationMessage()
                .setMessageId("intent_" + System.currentTimeMillis())
                .setTitle(this.getApplicationContext().getPackageName())
                .setContent("this is a intent notification")
                .setTargetEvent(intentUri);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

            
此代码块在浮窗中显示

Kotlin:

// 首先获取intentUri val intent = Intent() intent.setClassName(packageName, IntentActivity::class.java.canonicalName) intent.action = ExampleGlobal.ACTION_INTENT_NOTIFICATION val bundle = Bundle() bundle.putString("description", "this is a intent notification") bundle.putString("form", MainActivity::class.java.simpleName) bundle.putString("to", IntentActivity::class.java.simpleName) intent.putExtras(bundle) val intentUri = intent.toURI() // 之后发送自定义跳转通知 val notificationMessage = NotificationMessage() .setMessageId("intent_${System.currentTimeMillis()}") .setTitle(applicationContext.packageName) .setContent("this is a intent notification") .setContent(intentUri) MTPushPrivatesApi.showNotification(this, notificationMessage)
              
                      // 首先获取intentUri
        val intent = Intent()
        intent.setClassName(packageName, IntentActivity::class.java.canonicalName)
        intent.action = ExampleGlobal.ACTION_INTENT_NOTIFICATION
        val bundle = Bundle()
        bundle.putString("description", "this is a intent notification")
        bundle.putString("form", MainActivity::class.java.simpleName)
        bundle.putString("to", IntentActivity::class.java.simpleName)
        intent.putExtras(bundle)
        val intentUri = intent.toURI()
        // 之后发送自定义跳转通知
        val notificationMessage = NotificationMessage()
                .setMessageId("intent_${System.currentTimeMillis()}")
                .setTitle(applicationContext.packageName)
                .setContent("this is a intent notification")
                .setContent(intentUri)
        MTPushPrivatesApi.showNotification(this, notificationMessage)

            
此代码块在浮窗中显示

分组通知

  • 支持将多个通知分组显示,提供更好的用户体验
  • 相同group的通知会在通知栏中折叠显示
  • 必须设置group属性,且至少有一个通知设置groupSummary为true才能触发分组

Java:

// 1. 先发送组摘要通知 NotificationMessage summaryMessage = new NotificationMessage() .setMessageId("summary_" + System.currentTimeMillis()) .setTitle("聊天组") .setContent("3条新消息") .setGroupId("chat_group") .setGroupSummary(true); MTPushPrivatesApi.showNotification(this, summaryMessage); // 2. 发送普通通知,会被分组到chat_group中 NotificationMessage message1 = new NotificationMessage() .setMessageId("msg1_" + System.currentTimeMillis()) .setTitle("张三") .setContent("你好") .setGroupId("chat_group") .setGroupSummary(false); MTPushPrivatesApi.showNotification(this, message1); NotificationMessage message2 = new NotificationMessage() .setMessageId("msg2_" + System.currentTimeMillis()) .setTitle("李四") .setContent("在吗?") .setGroupId("chat_group") .setGroupSummary(false); MTPushPrivatesApi.showNotification(this, message2);
              
                      // 1. 先发送组摘要通知
        NotificationMessage summaryMessage = new NotificationMessage()
                .setMessageId("summary_" + System.currentTimeMillis())
                .setTitle("聊天组")
                .setContent("3条新消息")
                .setGroupId("chat_group")
                .setGroupSummary(true);
        MTPushPrivatesApi.showNotification(this, summaryMessage);

        // 2. 发送普通通知,会被分组到chat_group中
        NotificationMessage message1 = new NotificationMessage()
                .setMessageId("msg1_" + System.currentTimeMillis())
                .setTitle("张三")
                .setContent("你好")
                .setGroupId("chat_group")
                .setGroupSummary(false);
        MTPushPrivatesApi.showNotification(this, message1);

        NotificationMessage message2 = new NotificationMessage()
                .setMessageId("msg2_" + System.currentTimeMillis())
                .setTitle("李四")
                .setContent("在吗?")
                .setGroupId("chat_group")
                .setGroupSummary(false);
        MTPushPrivatesApi.showNotification(this, message2);

            
此代码块在浮窗中显示

Kotlin:

// 1. 先发送组摘要通知 val summaryMessage = NotificationMessage() .setMessageId("summary_${System.currentTimeMillis()}") .setTitle("聊天组") .setContent("3条新消息") .setGroupId("chat_group") .setGroupSummary(true) MTPushPrivatesApi.showNotification(this, summaryMessage) // 2. 发送普通通知,会被分组到chat_group中 val message1 = NotificationMessage() .setMessageId("msg1_${System.currentTimeMillis()}") .setTitle("张三") .setContent("你好") .setGroupId("chat_group") .setGroupSummary(false) MTPushPrivatesApi.showNotification(this, message1) val message2 = NotificationMessage() .setMessageId("msg2_${System.currentTimeMillis()}") .setTitle("李四") .setContent("在吗?") .setGroupId("chat_group") .setGroupSummary(false) MTPushPrivatesApi.showNotification(this, message2)
              
                      // 1. 先发送组摘要通知
        val summaryMessage = NotificationMessage()
                .setMessageId("summary_${System.currentTimeMillis()}")
                .setTitle("聊天组")
                .setContent("3条新消息")
                .setGroupId("chat_group")
                .setGroupSummary(true)
        MTPushPrivatesApi.showNotification(this, summaryMessage)

        // 2. 发送普通通知,会被分组到chat_group中
        val message1 = NotificationMessage()
                .setMessageId("msg1_${System.currentTimeMillis()}")
                .setTitle("张三")
                .setContent("你好")
                .setGroupId("chat_group")
                .setGroupSummary(false)
        MTPushPrivatesApi.showNotification(this, message1)

        val message2 = NotificationMessage()
                .setMessageId("msg2_${System.currentTimeMillis()}")
                .setTitle("李四")
                .setContent("在吗?")
                .setGroupId("chat_group")
                .setGroupSummary(false)
        MTPushPrivatesApi.showNotification(this, message2)

            
此代码块在浮窗中显示

分组通知参数说明

group

  • 通知的组ID,用于将多个通知分组显示
  • 相同group的通知会在通知栏中折叠显示,提供更好的用户体验
  • 必须设置,否则无法触发分组效果

groupSummary

  • 是否为组摘要通知,用于控制通知的折叠行为
  • true:表示这是一个组摘要通知,会显示组的概要信息
  • false:表示这是一个普通通知,会被折叠到组中
  • 至少需要有一个通知设置为true才能触发分组
Icon Solid Transparent White Qiyu
联系销售