本地通知訊息

最新更新:2022-11-29

低級通知

  • 沒有鈴聲 / 震動 /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);

        
此代碼塊在浮窗中顯示
在文档中心打开