[Android] I can't find a solution. 'Notification pop up' is required

[Android] I can’t find a solution. ‘Notification pop up’ is required.

Reference: [Android] 'Notification' does not work

‘Notification pop up’ doesn’t work after Android version is higher.
Can anyone solve it? Please help me. I want to solve

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

  1. I set up in my smartphone like the above site But
    ‘showNotification ();’ Does not work.

  2. What is the problem?

import android.os.Build;

import android.app.Notification;
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 = “notification_exam”;

void setup() {
fullScreen();
initNotifications();
}

void draw() {
background(150);
}

void mousePressed() {
println(“PRESS ACTION!”);
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;

// int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.enableVibration(true); // 진동 주기

NotificationManager notificationManager = (NotificationManager)
  getContext().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

println("CASE-A");

}else{
println(“CASE-B”);

}

}

void showNotification() {

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle(“This is a messsage”)
.setContentText(“Mouse pressed event detected!”)
// .setTicker(“Notification!”)
// .setColor(color(#FFE74C3C))
.setWhen(System.currentTimeMillis())
//.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_MAX) // PRIORITY_HIGH
.setOngoing(true)
.setAutoCancel(true);

mBuilder.setVibrate(new long[] { 1000, 1000});

// mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

//NotificationManager mNotifyMgr = (NotificationManager) this.getContext().getSystemService(NOTIFICATION_SERVICE);
//mNotifyMgr.notify(001, mBuilder.build());

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
int notificationId = 666;
notificationManager.notify(1, mBuilder.build());

}

Solve!!! Thanks. ^^

1 Like

Great! Did you also manage to get my popup menu code working?

1 Like

@GWAK ===
what have you changed in the code for solving?

@akenaton

mBuilder.setChannelId(CHANNEL_ID); << this

@noel
I’m trying now.
thank you.

@GWAK ==== i dont understand:
the previous code you posted was:

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

and it is the same… The unique explanation i can see is the first CHANNEL_ID uses a string with forbidden chars but in this case why it does not fire an error?

1 Like

@akenaton

I will tell you about my test.

  1. Processing Does not work on Android 3.0.
  2. Processing Works with Android 4.0.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (getContext ())

mBuilder.setChannelId (CHANNEL_ID);

The channel ID input was sent to mBuilder. I do not know why, but it worked.

@GWAK ===
sorry, i dont understand; what is this android 4 vs 3??? - You never spoke about that in your previous posts; in this type of case it is very difficult to help!

1 Like

@akenaton

Dear akenaton,
Good morning.

I’m talking about Android mode version.

In Android mode 3.0
'mBuilder.setChannelId (CHANNEL_ID); 'Error.

However, in Android mode 4.0
'mBuilder.setChannelId (CHANNEL_ID); ’
It works.

This solved ‘Notification’.

But there was a problem.
Text is broken after Android Mode 4.0
help me.

@akenaton I think he means this previous mode.

@noel ===
yes i think also, but the 2 codes are quite (the string declared for CHANNEL_ID is the unique change) the same and as he never speaks about 4.0/3.0 before how can you guess and help
BTW : i see that you have put in github something about “onPause()” bug; as i never got any problem with onPause() can you put some code snippet to verify?

1 Like

The code that verifie’s it is on the same issue in github.

1 Like