本地通知消息

最新更新:2022-12-20

低级通知

  • 没有铃声/震动/led灯
  • 仅在通知栏展示通知消息
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);

        
此代码块在浮窗中显示

普通通知

  • 有铃声/震动/led灯,可通过defaultProperties配置铃声/震动/led灯
  • 在通知栏展示通知消息
  • 在状态栏展示通知小图标
NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("normal_priority_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a normal priority notification") // 这里演示仅铃声和震动生效 .setDefaultProperties(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")
                // 这里演示仅铃声和震动生效
                .setDefaultProperties(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_DEFAULT);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

        
此代码块在浮窗中显示

高级通知

  • 有铃声/震动/led灯,可通过defaultProperties配置铃声/震动/led灯
  • 在通知栏展示通知消息
  • 在状态栏展示通知小图标
  • 设置应用允许悬浮窗后,可在屏幕顶部悬浮窗弹出
NotificationMessage notificationMessage = new NotificationMessage() .setMessageId("high_priority_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a high priority notification") // 这里演示铃声/震动/led灯全部生效 .setDefaultProperties(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灯全部生效
                .setDefaultProperties(Notification.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_HIGH);
        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);
                  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("inbox_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a inbox notification") .setStyle(MTPushPrivatesApi.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(MTPushPrivatesApi.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("big_picture_" + System.currentTimeMillis()) .setTitle(this.getApplicationContext().getPackageName()) .setContent("this is a big picture notification") .setStyle(MTPushPrivatesApi.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(MTPushPrivatesApi.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)设置最终角标数量
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);

        
此代码块在浮窗中显示

自定义铃声通知

  • 需要提前在res/raw/下放好对应铃声,这里以"coin.mp3"举例
  • Android8.0开始,铃声在channel中设置
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);

        
此代码块在浮窗中显示

自定义channel通知

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;
    }

        
此代码块在浮窗中显示

自定义布局通知

// 这里定义一个常量,代表自定义布局的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);

        
此代码块在浮窗中显示

自定义跳转通知

// 首先获取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()) .setContentText("this is a intent notification") .setContent(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())
                .setContentText("this is a intent notification")
                .setContent(intentUri);
        MTPushPrivatesApi.showNotification(this, notificationMessage);

        
此代码块在浮窗中显示
在文档中心打开
icon
联系销售