Android notification pls help

Hello i want thus and i found thus Code but don’t Work!


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());
}

main|400x500

I become rhe Error: ----------

  1. ERROR in /data/user/0/com.calsignlabs.apde/app_build/src/processing_test/notifications/nachriten.java (at line 69)
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext(), CHANNEL_ID)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    The constructor NotificationCompat.Builder(Context, String) is undefined

@GWAK or @noel @Andres @kfrajer

1 Like

@MonteliaRO

What about your version information?

Settings vary depending on the situation.

Reference site:

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());
}

  1. ERROR in /data/user/0/com.calsignlabs.apde/app_build/src/processing/test/sketch_20211110a/sketch_20211110a.java (at line 49)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    ^^^^^
    Build cannot be resolved

  1. ERROR in /data/user/0/com.calsignlabs.apde/app_build/src/processing/test/sketch_20211110a/sketch_20211110a.java (at line 49)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    ^^^^^
    Build cannot be resolved

Edit: now ist Work but i don’t get the notification when i Press @GWAK

@MonteliaRO

  1. What is your processing version?
  2. What is your Android version?
  3. What is the SDK version?

Should I tell you about it so that I can’t interpret it?

My Processing Version is: 0.5.1
Android ver: 10
SDK idk what this is