Message de notification locale
Notifications de niveau faible
- Pas de sonnerie/vibration/luminosité LED
- Affiche uniquement les messages de notification dans la barre de notifications
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("low_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification de faible priorité")
.setPriority(Notification.PRIORITY_LOW);
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("low_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification de faible priorité")
.setPriority(Notification.PRIORITY_LOW);
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification générale
- Sonnerie/Vibration/lumière LED disponibles via defaultProperties
- Affiche le message de notification dans la barre de notifications
- Affiche les icônes de notification dans la barre d'état
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("normal_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification de priorité normale")
// Ici, seule la sonnerie et la vibration fonctionnent
.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("il s'agit d'une notification de priorité normale")
// Ici, seule la sonnerie et la vibration fonctionnent
.setDefaultProperties(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_DEFAULT);
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification avancée
- Sonnerie/Vibration/lumière LED disponibles via defaultProperties
- Affiche le message de notification dans la barre de notifications
- Affiche les icônes de notification dans la barre d'état
- Après avoir autorisé l'application à afficher des fenêtres flottantes, elles peuvent apparaître en haut de l'écran
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("high_priority_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification de haute priorité")
// Ici, sonnerie/vibration/lumière LED fonctionnent
.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("il s'agit d'une notification de haute priorité")
// Ici, sonnerie/vibration/lumière LED fonctionnent
.setDefaultProperties(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH);
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification style grand texte
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("big_text_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification grand texte")
.setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
.setBigText("Adieu à Cambridge est un poème bien connu du poète moderne Xu Zhimo et une œuvre représentative de l'École de la Nouvelle Lune. Avec les hauts et les bas du sentiment lors du départ de Cambridge comme fil conducteur, tout le poème exprime le profond attachement à Cambridge pour d'autres raisons. Un langage léger et doux, une forme raffinée et mature, le poète dépeint une scène fluide par la méthode du virtuel et du réel, ce qui crée une belle ambiance partout. Il exprime soigneusement et subtilement l'amour du poète pour Cambridge, sa nostalgie de la vie passée et sa tristesse impuissante face au présent, ce qui constitue un chant vrai, riche et plein de sens dans les poèmes de Xu Zhimo.");
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("big_text_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification grand texte")
.setStyle(NotificationMessage.NOTIFICATION_STYLE_BIG_TEXT)
.setBigText("Adieu à Cambridge est un poème bien connu du poète moderne Xu Zhimo et une œuvre représentative de l'École de la Nouvelle Lune. Avec les hauts et les bas du sentiment lors du départ de Cambridge comme fil conducteur, tout le poème exprime le profond attachement à Cambridge pour d'autres raisons. Un langage léger et doux, une forme raffinée et mature, le poète dépeint une scène fluide par la méthode du virtuel et du réel, ce qui crée une belle ambiance partout. Il exprime soigneusement et subtilement l'amour du poète pour Cambridge, sa nostalgie de la vie passée et sa tristesse impuissante face au présent, ce qui constitue un chant vrai, riche et plein de sens dans les poèmes de Xu Zhimo.");
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification style boîte de réception
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("inbox_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification boîte de réception")
.setStyle(MTPushPrivatesApi.NOTIFICATION_STYLE_INBOX)
.setInbox(new String[]{"voici la première boîte de réception", "voici la deuxième boîte de réception", "voici la troisième boîte de réception"});
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("inbox_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification boîte de réception")
.setStyle(MTPushPrivatesApi.NOTIFICATION_STYLE_INBOX)
.setInbox(new String[]{"voici la première boîte de réception", "voici la deuxième boîte de réception", "voici la troisième boîte de réception"});
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification style grande image
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("big_picture_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification grande image")
.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("il s'agit d'une notification grande image")
.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);
Afficher ce bloc de code dans la fenêtre flottante
Notification badge personnalisée
- Prend en charge uniquement Huawei/Honor/XIAOMI
- Sur un appareil Xiaomi, lors de la suppression d'une notification, le nombre de badges diminue automatiquement de 1
- Sur les appareils Huawei/Honor, lors de la suppression d'une notification, le nombre de badges ne diminue pas automatiquement de 1. Il faut appeler MTPushPrivatesApi.setNotificationBadge(context, number) pour définir le nombre final de badges
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("badge_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification badge")
// Logique cumulative, où le nombre de badges + 1
.setBadge(1);
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("badge_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification badge")
// Logique cumulative, où le nombre de badges + 1
.setBadge(1);
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification avec sonnerie personnalisée
- La sonnerie correspondante doit être placée à l'avance dans res/raw/, par exemple, "coin.mp3"
- À partir d'Android 8.0, les sonneries sont définies dans le canal
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("sound_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification sonore")
.setSound("coin");
MTPushPrivatesApi.showNotification(this, notificationMessage);
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("sound_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification sonore")
.setSound("coin");
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notifications avec canal personnalisé
NotificationChannel notificationChannel = buildChannel();
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("channel_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification canal")
.setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId());
MTPushPrivatesApi.showNotification(this, notificationMessage);
// Démonstration de création d'un canal de niveau normal avec la sonnerie 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";
// id du canal
String channelId = "money123";
// nom du canal
String channelName = "money";
// Priorité du canal
int importance = NotificationManager.IMPORTANCE_DEFAULT;
// description du canal
String channelDescription = "Utilisé pour démontrer le canal, avec des sonneries personnalisées";
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 + "] existe déjà");
}
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("il s'agit d'une notification canal")
.setChannelId(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : notificationChannel.getId());
MTPushPrivatesApi.showNotification(this, notificationMessage);
// Démonstration de création d'un canal de niveau normal avec la sonnerie 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";
// id du canal
String channelId = "money123";
// nom du canal
String channelName = "money";
// Priorité du canal
int importance = NotificationManager.IMPORTANCE_DEFAULT;
// description du canal
String channelDescription = "Utilisé pour démontrer le canal, avec des sonneries personnalisées";
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 + "] existe déjà");
}
return channel;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
Afficher ce bloc de code dans la fenêtre flottante
Notification avec mise en page personnalisée
// Une constante est définie ici, représentant le builderId de la mise en page personnalisée
private static final int BUILDER_ID = 1001;
// Définir d'abord la mise en page personnalisée dans le SDK cloud privé 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);
// Envoyer ensuite la notification avec mise en page personnalisée
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("custom_layout_" + System.currentTimeMillis())
.setBuilderId(BUILDER_ID)
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification avec mise en page personnalisée");
MTPushPrivatesApi.showNotification(this, notificationMessage);
// Une constante est définie ici, représentant le builderId de la mise en page personnalisée
private static final int BUILDER_ID = 1001;
// Définir d'abord la mise en page personnalisée dans le SDK cloud privé 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);
// Envoyer ensuite la notification avec mise en page personnalisée
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("custom_layout_" + System.currentTimeMillis())
.setBuilderId(BUILDER_ID)
.setTitle(this.getApplicationContext().getPackageName())
.setContent("il s'agit d'une notification avec mise en page personnalisée");
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante
Notification avec action personnalisée
// Récupérer d'abord l'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", "il s'agit d'une notification intent");
bundle.putString("form", MainActivity.class.getSimpleName());
bundle.putString("to", IntentActivity.class.getSimpleName());
intent.putExtras(bundle);
String intentUri = intent.toURI();
// Puis envoyer une notification avec action personnalisée
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("intent_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContentText("il s'agit d'une notification intent")
.setContent(intentUri);
MTPushPrivatesApi.showNotification(this, notificationMessage);
// Récupérer d'abord l'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", "il s'agit d'une notification intent");
bundle.putString("form", MainActivity.class.getSimpleName());
bundle.putString("to", IntentActivity.class.getSimpleName());
intent.putExtras(bundle);
String intentUri = intent.toURI();
// Puis envoyer une notification avec action personnalisée
NotificationMessage notificationMessage = new NotificationMessage()
.setMessageId("intent_" + System.currentTimeMillis())
.setTitle(this.getApplicationContext().getPackageName())
.setContentText("il s'agit d'une notification intent")
.setContent(intentUri);
MTPushPrivatesApi.showNotification(this, notificationMessage);
Afficher ce bloc de code dans la fenêtre flottante

