ข้อความแจ้งเตือนในเครื่อง
การแจ้งเตือนระดับต่ำ
- ไม่มีเสียง/การสั่น/ไฟ LED
- แสดงข้อความแจ้งเตือนในแถบการแจ้งเตือนเท่านั้น
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("low_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนระดับความสำคัญต่ำ")
.setPriority(Notification.PRIORITY_LOW);
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("low_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนระดับความสำคัญต่ำ")
.setPriority(Notification.PRIORITY_LOW);
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนทั่วไป
- สามารถตั้งค่าเสียง/การสั่น/ไฟ LED ได้ผ่าน defaultProperties
- แสดงข้อความแจ้งเตือนในแถบการแจ้งเตือน
- แสดงไอคอนแจ้งเตือนในแถบสถานะ
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("normal_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนระดับความสำคัญปกติ")
// ตัวอย่างนี้แสดงเฉพาะเสียงและการสั่น
.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("นี่คือการแจ้งเตือนระดับความสำคัญปกติ")
// ตัวอย่างนี้แสดงเฉพาะเสียงและการสั่น
.setDefaultProperties(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_DEFAULT);
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนระดับสูง
- สามารถตั้งค่าเสียง/การสั่น/ไฟ LED ได้ผ่าน defaultProperties
- แสดงข้อความแจ้งเตือนในแถบการแจ้งเตือน
- แสดงไอคอนแจ้งเตือนในแถบสถานะ
- หากแอปพลิเคชันอนุญาตให้แสดงหน้าต่างลอย จะสามารถแสดงที่ด้านบนของหน้าจอได้
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("high_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนระดับความสำคัญสูง")
// ตัวอย่างนี้แสดงเสียง/การสั่น/ไฟ 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("นี่คือการแจ้งเตือนระดับความสำคัญสูง")
// ตัวอย่างนี้แสดงเสียง/การสั่น/ไฟ 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("นี่คือการแจ้งเตือนแบบข้อความยาว")
.setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
.setBigText("《再别康桥》เป็นบทกวีที่มีชื่อเสียงของนักกวีสมัยใหม่ 徐志摩 ซึ่งเป็นหนึ่งในผลงานตัวแทนของกลุ่ม New Moon บทกวีทั้งหมดใช้ความรู้สึกที่เปลี่ยนแปลงเมื่ออำลาสะพานแคมบริดจ์เป็นเส้นเรื่อง แสดงถึงความรักลึกซึ้งต่อสะพานแคมบริดจ์ ภาษาเบาและงดงาม รูปแบบประณีตและสมบูรณ์แบบ กวีใช้วิธีการผสมผสานระหว่างความจริงและจินตนาการในการวาดภาพที่เคลื่อนไหว สร้างบรรยากาศที่งดงามในทุกจุด เขาแสดงออกถึงความรักต่อสะพานแคมบริดจ์ ความระลึกถึงชีวิตในอดีต และความเศร้าใจในความเป็นจริงอย่างละเอียดและชาญฉลาด เป็นบทกวีที่จริงใจ อุดมสมบูรณ์ และมีความหมายในผลงานของ 徐志摩");
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("big_text_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบข้อความยาว")
.setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
.setBigText("《再别康桥》เป็นบทกวีที่มีชื่อเสียงของนักกวีสมัยใหม่ 徐志摩 ซึ่งเป็นหนึ่งในผลงานตัวแทนของกลุ่ม New Moon บทกวีทั้งหมดใช้ความรู้สึกที่เปลี่ยนแปลงเมื่ออำลาสะพานแคมบริดจ์เป็นเส้นเรื่อง แสดงถึงความรักลึกซึ้งต่อสะพานแคมบริดจ์ ภาษาเบาและงดงาม รูปแบบประณีตและสมบูรณ์แบบ กวีใช้วิธีการผสมผสานระหว่างความจริงและจินตนาการในการวาดภาพที่เคลื่อนไหว สร้างบรรยากาศที่งดงามในทุกจุด เขาแสดงออกถึงความรักต่อสะพานแคมบริดจ์ ความระลึกถึงชีวิตในอดีต และความเศร้าใจในความเป็นจริงอย่างละเอียดและชาญฉลาด เป็นบทกวีที่จริงใจ อุดมสมบูรณ์ และมีความหมายในผลงานของ 徐志摩");
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนแบบกล่องข้อความ
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("inbox_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบกล่องข้อความ")
.setStyle(MTPushPrivatesApi.NOTIFICATION_STYLE_INBOX)
.setInbox(new String[]{"นี่คือกล่องข้อความที่หนึ่ง", "นี่คือกล่องข้อความที่สอง", "นี่คือกล่องข้อความที่สาม"});
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("inbox_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบกล่องข้อความ")
.setStyle(MTPushPrivatesApi.NOTIFICATION_STYLE_INBOX)
.setInbox(new String[]{"นี่คือกล่องข้อความที่หนึ่ง", "นี่คือกล่องข้อความที่สอง", "นี่คือกล่องข้อความที่สาม"});
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนแบบรูปภาพขนาดใหญ่
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("big_picture_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบรูปภาพขนาดใหญ่")
.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("นี่คือการแจ้งเตือนแบบรูปภาพขนาดใหญ่")
.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);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนแบบไอคอนมุมที่กำหนดเอง
- รองรับเฉพาะ Huawei/Honor/Xiaomi
- บนอุปกรณ์ Xiaomi เมื่อการแจ้งเตือนถูกลบ จำนวนไอคอนมุมจะลดลงโดยอัตโนมัติ 1
- บนอุปกรณ์ Huawei/Honor เมื่อการแจ้งเตือนถูกลบ จำนวนไอคอนมุมจะไม่ลดลงโดยอัตโนมัติ ต้องเรียกใช้ MTPushPrivatesApi.setNotificationBadge(context, number) เพื่อกำหนดจำนวนไอคอนมุมสุดท้าย
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("badge_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบไอคอนมุม")
// เพิ่มจำนวนไอคอนมุม +1
.setBadge(1);
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("badge_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบไอคอนมุม")
// เพิ่มจำนวนไอคอนมุม +1
.setBadge(1);
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนแบบเสียงที่กำหนดเอง
- ไฟล์เสียงที่เกี่ยวข้องต้องวางไว้ล่วงหน้าในไดเรกทอรี res/raw/ เช่น "coin.mp3"
- ตั้งแต่ Android 8.0 เป็นต้นไป เสียงจะถูกตั้งค่าในช่องทาง
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("sound_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบเสียง")
.setSound("coin");
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("sound_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนแบบเสียง")
.setSound("coin");
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนช่องทางที่กำหนดเอง
NotificationChannel notificationChannel = buildChannel();
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("channel_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("นี่คือการแจ้งเตือนช่องทาง")
.setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId());
MTPushPrivatesApi.showNotification(this, notificationMessage);
// แสดงตัวอย่างวิธีสร้างช่องทางระดับปกติพร้อมเสียงริงโทนแบบกำหนดเอง
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";
// รหัสช่องทาง
String channelId = "money123";
// ชื่อช่องทาง
String channelName = "money";
// ลำดับความสำคัญของช่องทาง
int importance = NotificationManager.IMPORTANCE_DEFAULT;
// คำอธิบายช่องทาง
String channelDescription = "ใช้เพื่อแสดงตัวอย่างช่องทาง พร้อมเสียงเรียกเข้าที่กำหนดเอง";
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 + "] มีอยู่แล้ว");
}
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("นี่คือการแจ้งเตือนช่องทาง")
.setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId());
MTPushPrivatesApi.showNotification(this, notificationMessage);
// แสดงตัวอย่างวิธีสร้างช่องทางระดับปกติพร้อมเสียงริงโทนแบบกำหนดเอง
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";
// รหัสช่องทาง
String channelId = "money123";
// ชื่อช่องทาง
String channelName = "money";
// ลำดับความสำคัญของช่องทาง
int importance = NotificationManager.IMPORTANCE_DEFAULT;
// คำอธิบายช่องทาง
String channelDescription = "ใช้เพื่อแสดงตัวอย่างช่องทาง พร้อมเสียงเรียกเข้าที่กำหนดเอง";
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 + "] มีอยู่แล้ว");
}
return channel;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
โค้ดนี้โชว์เป็นหน้าต่างลอย
การแจ้งเตือนเลย์เอาต์ที่กำหนดเอง
// กำหนดค่าคงที่ที่นี่ ซึ่งแสดงถึง builderId ของเลย์เอาต์ที่กำหนดเอง
private static final int BUILDER_ID = 1001;
// ตั้งค่าเลย์เอาต์ที่กำหนดเองไปยัง SDK คลาวด์ส่วนตัวของ Engagelab
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("นี่คือการแจ้งเตือนเลย์เอาต์ที่กำหนดเอง");
MTPushPrivatesApi.showNotification(this, notificationMessage);
// กำหนดค่าคงที่ที่นี่ ซึ่งแสดงถึง builderId ของเลย์เอาต์ที่กำหนดเอง
private static final int BUILDER_ID = 1001;
// ตั้งค่าเลย์เอาต์ที่กำหนดเองไปยัง SDK คลาวด์ส่วนตัวของ Engagelab
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("นี่คือการแจ้งเตือนเลย์เอาต์ที่กำหนดเอง");
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", "นี่คือการแจ้งเตือนแบบ intent");
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("นี่คือการแจ้งเตือนแบบ intent")
.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", "นี่คือการแจ้งเตือนแบบ intent");
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("นี่คือการแจ้งเตือนแบบ intent")
.setContent(intentUri);
MTPushPrivatesApi.showNotification(this, notificationMessage);
โค้ดนี้โชว์เป็นหน้าต่างลอย