Android Notifications

EXAM LINK : https://www.androidauthority.com/how-to-create-android-notifications-707254/

I want to display a notification as shown in the figure.

  1. Is it possible to processing at android mode?

  2. If possible, please give us some example source code. help me!

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:

I hope this is useful. Maybe the android mode should include the support libraries so there are no errors in the PDE when importing them…

2 Likes

@Andres

Thank you for your kind reply.

The following error occurred.

Why is this happening?


  1. 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

  1. 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

  1. 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


  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

9 problems (6 errors, 3 warnings)

Hi @GWAK

Just to be sure, did you post this comment or was it automatically generated?

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.

For instance:

Check: java - Android studio Cannot resolve symbol 'NotificationChannel' - Stack Overflow

For error #5 above:

android - Cannot be resolved or is not a field for SUPPORTED_ABIS,FLAVOR & VERSION_CODES.LOLLIPOP - Stack Overflow
Build.VERSION_CODES  |  Android Developers

And so on.

Kf

Also here is the manifest. You do not need to write your own manifest. JUst use the one provided by Processing Android.

Kf

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="26"/>
    <application android:icon="@drawable/icon" android:label="">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

@dan850

Hello, ^^
It is a mistake. Thank you.

@kfrajer @Andres

It’s a genius. Thank you very much. It has been resolved.
The conclusion is that API26 + works.
Thanks.

@kfrajer
@Andres

Thanks to you, I was able to finish.
Thank you.

1 Like