Why my notification doesn't appear - android

Hello guys Im using advance notification manager this is the code
Notification note = new Notification();
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notif);
contentView.setImageViewResource(R.id.image, R.drawable.notif);
contentView.setTextViewText(R.id.title, "Focused Crawling");
contentView.setTextViewText(R.id.text, "Crawling In Progress....");
note.contentView = contentView;
Intent notificationIntent = new Intent(this, LoadingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
note.contentIntent = contentIntent;
note.vibrate = new long[] { 500L, 200L, 200L, 500L };
note.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
note.flags |= Notification.FLAG_NO_CLEAR;
mgr.notify(1337, note);
startForeground(1337, note);
and why it didnt appear at the status bar ?

You don't specify an icon for the status bar, which is required.
Add a line like this to your code:
note.icon = R.drawable.youricon;
See Creating a Notification

Related

Notification coloured action button Android 10

I'm trying to color the buttons(Action) in Notification like this.
So far this is what i'm achieved so far.
Below is the code i'm using
NotificationService.class
private void showCallNotification(Map<String, String> dataMap) {
notificationId = (int) (System.currentTimeMillis() % 10000);
Intent intent = new Intent(getApplicationContext(), ReceiveCallActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction(Intent.ACTION_ANSWER)
.putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_ACCEPTED)
.putExtra("title", dataMap.get("title"))
.putExtra("action", dataMap.get("action"));
Intent cancelIntent = new Intent(getApplicationContext(), VideoCallReceiver.class)
.setAction(AppConstants.INCOMING_CALL_BROADCAST_ACTION)
.putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_DECLINED)
.putExtra("notificationId", notificationId);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action declineAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.accept_call), pendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(dataMap.get("sender"))
.setContentText(getString(R.string.incoming_call))
.setColor(getResources().getColor(R.color.notification_color))
.setAutoCancel(true)
.setTimeoutAfter(CALL_DISMISS_TIME)
.addAction(declineAction)
.addAction(acceptAction)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(pendingIntent, true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createCallNotificationChannel();
}
notificationManager.notify(notificationId, builder.build());
}
I'm out of ideas now. Any help will be appreciated.
Code taken from the Android official Dialer app
when you set the action string getString(R.string.decline_call) change that with a call to the method
private Spannable getActionText(#StringRes int stringRes, #ColorRes int colorRes) {
Spannable spannable = new SpannableString(context.getText(stringRes));
if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
spannable.setSpan(
new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
}
return spannable;
}
I have managed to apply colors for the notification action buttons. Use spannable just like Aeron suggested (code from the Dialer app) and the very important thing is to set USE_FULL_SCREEN_INTENT permission. Otherwise colors are ignored in the dark mode or look incorrect in the non dark mode.
You can use custom layout in your notification. There are setCustomContentView() and setCustomBigContentView methods:
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNE L_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
Do have a read here
Use custom drawable like this
NotificationCompat.Action declineAction = new NotificationCompat.Action(R.drawable.ic_red, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(R.drawable.ic_green, getString(R.string.accept_call), pendingIntent);

MediaPlayer Notification Controls Not Working

I have the following code that creates an ongoing notification for my music player
private Notification buildNotification(String title, String artist) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);
Intent previousTrackIntent = new Intent(Music.getContext(), MusicService.class);
previousTrackIntent.setAction(MusicService.ACTION_REWIND);
PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, previousTrackIntent, 0);
Intent playPauseTrackIntent = new Intent(Music.getContext(), MusicService.class);
playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, playPauseTrackIntent, 0);
Intent nextTrackIntent = new Intent(Music.getContext(), MusicService.class);
nextTrackIntent.setAction(MusicService.ACTION_SKIP);
PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, nextTrackIntent, 0);
Intent stopServiceIntent = new Intent(Music.getContext(), MusicService.class);
stopServiceIntent.setAction(MusicService.ACTION_STOP);
PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, stopServiceIntent, 0);
if (mState == State.Playing) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
} else if (mState == State.Paused || mState == State.Stopped) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
}
compactNotification.setTextViewText(R.id.notification_base_line_one, title);
compactNotification.setTextViewText(R.id.notification_base_line_two, artist);
expandedNotification.setTextViewText(R.id.notification_base_line_one, title);
expandedNotification.setTextViewText(R.id.notification_base_line_two, artist);
compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
//Set the album art.
expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
compactNotification.setImageViewBitmap(R.id.notification_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
//Attach the shrunken layout to the notification.
notificationBuilder.setContent(compactNotification);
//Build the notification object.
Notification notification = notificationBuilder.build();
//Attach the expanded layout to the notification and set its flags.
if (Build.VERSION.SDK_INT >= 16)
notification.bigContentView = expandedNotification;
notification.flags = Notification.FLAG_FOREGROUND_SERVICE |
Notification.FLAG_NO_CLEAR |
Notification.FLAG_ONGOING_EVENT;
return notification;
}
The notification appears correctly but the controls dont work even thought the controls are well defined.
Is there something else I need to enable or assign to get the controls to work?
I know this answer is 5 years late, but I found that Notification.FLAG_FOREGROUND_SERVICE still shows the controls, but doesn't allow them to be "clickable". I found the same behavior when I showed the notification with startForeground(int,notification);
If I just used the following flags (Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT), then call stopForeground(false); and then nm.notify(int, notification); it works as expected.

Change button background on click event , notification bar

i want to Change button background on click event , notification bar
for eg. i have button with text play which is on notification bar when i click on play it should be change to pause and again if i click on pause this pause text should be changed to play text, how can we do it any help is appreciated
thanks in advance
it is more like music player has on going notification so we can play and pause the media using notification bar as well.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentv = new RemoteViews(getPackageName(),
R.layout.notification_bar);
int icon = R.drawable.noti_launcher;
CharSequence tickerText = "notification";
long when = System.currentTimeMillis(); // now
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
notification.contentView = contentv;
Intent laucherIntent = new Intent(this, NavigationActivity.class);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
PendingIntent launcherPI = PendingIntent.getActivity(this, 0,
laucherIntent, 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Intent flaotIntent = new Intent(this, FloatMainActivity.class);
PendingIntent floatPI = PendingIntent.getActivity(this, 0, flaotIntent,
0);
PendingIntent wifiPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_WIFI), 0);
PendingIntent btPI = PendingIntent.getBroadcast(context, 0, new Intent(
Constants.ACTION_BLUETOOTH), 0);
PendingIntent mobileDataPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_DATA), 0);
PendingIntent alarmsPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.CUSTOM_ACTION), 0);
contentv.setImageViewResource(R.id.bluetooth, R.drawable.noti_wifi);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentv.setOnClickPendingIntent(R.id.bluetooth, btPI);
contentv.setOnClickPendingIntent(R.id.wifi, wifiPI);
contentv.setOnClickPendingIntent(R.id.data, mobileDataPI);
contentv.setOnClickPendingIntent(R.id.alarm, pi);
contentv.setOnClickPendingIntent(R.id.icon, launcherPI);
contentv.setOnClickPendingIntent(R.id.float_notification, floatPI);
notificationManager.notify(1, notification);
contentv.setInt(R.id.viewId, "change", R.drawable.nextId);
you can use this for change background image.

how to cache remote view?

i need to know if is it possible to cache layout remote view ?
if so how i can cache or clean the cache is there any example here i tried to google it i could't find an example how to cache layout remote view
for example how to cache this notification progress !
thank you for your time
updateTask = this;
//setup notification ID and information
Random r = new Random();
NOTIFICATION_ID = r.nextInt(80-65) + 1;
NOTIFICATION_ID++;
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
notification = new Notification(icon, getString(R.string.app_name), when);
contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setTextViewText(R.id.text, "cool"+NOTIFICATION_ID);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent();
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setAction(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(BabupMain.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);

Android custom notification bar

I do own according to the notification bar: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
I do like the tutorial :
Notification notification = new Notification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.icon = icon;
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.b_10);
contentView.setTextViewText(R.id.title, "Custom zgłoszenie");
contentView.setTextViewText(R.id.text, "test test test");
notification.contentView = contentView;
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(BATTERY_ID, notification);
There are errors in the lines:
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
Errors:
NotificationIntent cannot be resolved to a type
Multiple markers at this line
- ContentIntent cannot be resolved to
a type
- ta cannot be resolved to a variable
contentIntent cannot be resolved to a variable
Replace NotificationIntent with Intent (android does not provide NotificationIntent, unless its a custom class).
what you are looking for is
Intent notificationIntent = new Intent(BatteryInfoService.this,BatteryInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Please read the tutorial carefully. Intent and PendingIntent are android Classes where as NotificationIntent and ContentIntent are not. If you have made custom classes under those names and you can import the appropriate packages.

Categories

Resources