import android.os.Build;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import static android.content.Context.NOTIFICATION_SERVICE;
String CHANNEL_ID = "processing_test.notifications";
void setup() {
fullScreen();
initNotifications();
}
void draw() {
background(150);
}
void mousePressed() {
showNotification();
}
void initNotifications() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String name = "Manager";
String description = "This is problem.";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system
NotificationManager notificationManager = (NotificationManager)
getContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
}
void showNotification() {
// Create an explicit intent for an Activity in your app
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle("This is a messsage")
.setContentText("Mouse pressed event detected!")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
// Show the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
// notificationId is a unique int for each notification that you must define
int notificationId = 666;
notificationManager.notify(notificationId, mBuilder.build());
}
summary :
- There is no debug problem.
- Works well on Android version 6.0 smartphone.
(The Notification screen appears.) - Does not work on Android version 9 smartphones.
(Notification screen does not appear.)
Why?
Hardware Information:
- Samsung Galaxy S10
- Android Version 9
- Check the application notification settings