Notification not showing in android Oreo when I use .setStyle - java

When creating a notification in android Oreo everything works until I use .setStyle then it stops working
Code:
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
{
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setSmallIcon(R.drawable.icon)
.setOnlyAlertOnce(true)//show notification for only first time
.setLights(Color.CYAN, 500, 1200) //Setting the color of the led blink on the phone for notifications
.addAction(drw_previous, "Previous", pendingIntentPrevious) //Setting action for the previous button
.addAction(playbutton, "Play", pendingIntentPlay) //Setting the action for the play/pause button
.addAction(drw_next, "Next", pendingIntentNext) //Setting the action for he next button
.setDeleteIntent(pendingIntentDelete) //Setting delete intent for when the notification is destroyed/deleted
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken())) //Setting how and what order the buttons will be shown
.setShowWhen(false) //Do not show time for when this notification was created
.setDefaults(Notification.DEFAULT_ALL); //Defaults
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
Am I missing something. I have everything that should make it work. It used to work, but I am revisiting code changing it and making it better and not sure what went wrong this time. Your help is appreciated!

Related

Prevent a notification from being automatically hidden

I am using this to show notifications:
public static void showAlarmNotification(Context context, Class<?> cls, String title, String content, int idNotification){
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(idNotification, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).setPriority(NotificationCompat.PRIORITY_MAX);
Notification notification = builder.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setOngoing(true)
.setVibrate(new long[] {1000, 1000, 1000})
.setSmallIcon(R.drawable.ic_stat_name)
.setChannelId(NOTIFICATION_CHANNEL_ID)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALARM, context.getString(R.string.alarms), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null,null);
notificationChannel.setVibrationPattern(new long[] {1000, 1000, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(idNotification, notification);
}
It works fine to display a notification. But I want the notification not to disappear automatically as it normally happens, I want it to only go up if the user swipes it up, I have seen applications doing that but I have not found a way to do it
UPDATE
This example shows a notification that is not hidden until the user swipes it up
Example (YouTube)
That appears to be a notification with a set category of CATEGORY_ALARM.
See also system-wide category
The solution is to change setContentIntent to setFullScreenIntent, this is the source

Android NotificationManager not alerting user

I have the same issue as this person: NotificationManager Error Android Studio
However I already have the solution implemented a while ago but this just stopped working. I am at build API 28 on a P10 with Oreo 8.0.0 installed. My code executes the notification logic, but I get this error:
E/NotificationManager: notifyAsUser: tag=null, id=-1522221101, user=UserHandle{0}
and even though the icon for the notification pops up in the phone's notification bar, and tapping it opens my intent, the user gets no notification that this message arrived. I tried with the app in the foreground and in the background - both cases the small icon appears in the notification bar on top of the phone's UI, but no sound and no message.
Here is my code:
Intent intent = new Intent(getContext(), AuthRequestActivity.class);
intent.putExtra(getContext().getPackageName().concat(".param0"), code);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default", "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("ALP Push Notifications");
notificationManager.createNotificationChannel(channel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "default").setSmallIcon(R.drawable.app_icon)
.setContentTitle("Authentication Request").setContentText(code)
.setAutoCancel(true).setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getContext());
// Below alternative makes no difference
// NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
What am I doing wrong?

Android: Notification that requires user interaction / input

I'm programming some Android App that must make some Notification/Alarm after event occurs. Im wondering is there any function for NotificationManager like requireInteraction()?
Now when the certain event occurs the app just shows one notification for 1 sec, that's it..i'd like user to click OK to stop this vibration/sound
I found some code for notification from here:
NotificationCompat.Builder deprecated in Android O
Thanks #Mehul
public void showNotification (String from, String notification,
Intent intent) {
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
requestID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/requestID, mNotification);
}
This one shows notification and it doesnt wait for user Input
If there is a need to add the buttons or action after clicking on notification you can use in your builder:
To add an button:
.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));
or to add action that happen after user click the notification
.setContentIntent(*intent*);
Check the documentation about tab action and actions if you need more details.

android all notification disappeared when open the app

I'am using FCM for getting new notification which I am able to show without any problem but when i open the app all my notification disappeared
this my code for creating new notification
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext,mContext.getString(R.string.default_notification_channel_id));
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mContext.getString(R.string.default_notification_channel_id), name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
when click on a notification it opens an activity with extra data in
my intent
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", "wwwwwwww");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
now when i click on any notification it opens MainActivity with some extra value on which Main activity depends.
But when i click on a notification and it launches the activity all other notification disappeared.So, how can i prevent that and hide only clicked notification so user can see other notification
not quite sure whether it'll work or not but can u please try with setting the request code unique rather than 0.
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
replace 0 with the following
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
try this
**.setPriority(NotificationManager.IMPORTANCE_HIGH)**
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(ICON)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notification);

Android How to show alert on another app

Hi I want to show alert after click on my notification. Here is code:
#SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());
notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;
notification.vibrate = new long[]{100, 120, 100, 120};
Intent notificationIntent = new Intent(this, TerminarzActivity.class);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}
And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?
I know i must add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
but nothing else..
Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your MainActivity.
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
This code might help you, but still it depends what do you mean by "alert".

Categories

Resources