It is possible, but the problem is that the message API requires the android.support.v4 libraries that are not included with the platform SDK. Anyways… if you have this code in the PDE:
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 = "Test Notifications";
String description = "Notifications from Processing sketch are shown in this channel";
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(), CHANNEL_ID)
.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());
}
it will give you a bunch of errors in the PDE (“The import android.support.v4 cannot be resolved” etc.) but you should still be able to run it because these dependencies will be resolved when the sketch is compiled, and get the notification:
WARNING in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\MainActivity.java (at line 11)
import android.support.v4.content.ContextCompat;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import android.support.v4.content.ContextCompat is never used
WARNING in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\MainActivity.java (at line 15)
import android.Manifest;
^^^^^^^^^^^^^^^^
The import android.Manifest is never used
WARNING in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\MainActivity.java (at line 49)
int check;
^^^^^
The value of the local variable check is not used
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 9)
import android.app.NotificationChannel;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import android.app.NotificationChannel cannot be resolved
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 52)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
^
O cannot be resolved or is not a field
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 57)
int importance = NotificationManager.IMPORTANCE_HIGH;
^^^^^^^^^^^^^^^
IMPORTANCE_HIGH cannot be resolved or is not a field
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 58)
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
^^^^^^^^^^^^^^^^^^^
NotificationChannel cannot be resolved to a type
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 58)
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
^^^^^^^^^^^^^^^^^^^
NotificationChannel cannot be resolved to a type
ERROR in C:\Users\SJGWAK\AppData\Local\Temp\android6928576046861478137sketch\src\processing\test\notifications\Notifications.java (at line 69)
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext(), CHANNEL_ID)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor NotificationCompat.Builder(Context, String) is undefined
Notice you have warnings and errors. You can ignore the warnings. You need to focus on the errors. You first attempt is to google these errors as this is exactly what most people here will do when they try to help. Give it a try and see if you can find the solution yourself first, as these problems are likely common and already have some suggestions on how to resolve them.