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);
Related
I am trying to create a navigation notification android auto screen from the mobile app context. I tried the following code from main activity to trigger the notification. but it only shows on the mobile.
private void sendNotification(CharSequence title, CharSequence text, String channelId,
CharSequence channelName, int notificationId, int importance) {
CarNotificationManager carNotificationManager =
CarNotificationManager.from(this);
NotificationChannelCompat channel = new NotificationChannelCompat.Builder(channelId,
importance).setName(channelName).build();
carNotificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(this, channelId);
builder.setCategory(NotificationCompat.CATEGORY_NAVIGATION);
builder.setOngoing(true);
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title + " (phone)")
.setContentText(text + " (phone)")
.setColor(getColor(androidx.car.app.R.color.carColorGreen))
.setColorized(true)
.setLargeIcon(
BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher_foreground))
.addAction(
new NotificationCompat.Action.Builder(
R.drawable.ic_launcher_foreground,
"Action1 (phone)",
createPendingIntent(INTENT_ACTION_PRIMARY_PHONE))
.build())
.addAction(
R.drawable.ic_launcher_foreground,
"Action2 (phone)",
createPendingIntent(INTENT_ACTION_SECONDARY_PHONE))
.extend(
new CarAppExtender.Builder()
.setContentTitle(title)
.setContentText(text)
.setColor(CarColor.PRIMARY)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(
BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_launcher_foreground))
.addAction(
R.drawable.ic_launcher_foreground,
"navigate",
getPendingIntentForNavigation())
.addAction(
R.drawable.ic_launcher_foreground,
"ti",
createPendingIntentForCall())
.build());
carNotificationManager.notify(notificationId, builder);
}
private PendingIntent createPendingIntentForCall() {
Intent intent = new Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:+14257232350"));
return CarPendingIntent.getCarApp(this, intent.hashCode(), intent, 0);
}
/**
* Returns a pending intent with the provided intent action.
*/
private PendingIntent createPendingIntent(String intentAction) {
Intent intent = new Intent(intentAction);
return PendingIntent.getBroadcast(this, intentAction.hashCode(), intent,
PendingIntent.FLAG_MUTABLE);
}
private PendingIntent getPendingIntentForNavigation() {
Intent intent = new Intent(CarContext.ACTION_NAVIGATE).setData(Uri.parse("geo:0,0?q=Home"));
return CarPendingIntent.getCarApp(this, intent.hashCode(), intent, 0);
}
Here I tried set type as navigation. Also set other requirements for the navigation notification .
I am trying to start an activity from a notification. Upon starting that activity, I add data via intent.putextra to the intent so the activity shows the right content for the situation.
The activity that is being started is supposed to be open only once in the stack. I did achieve this via
android:launchMode="singleTop"
in my manifest.
However - and now I come to my question - if this activity is already running, I want it to close and replace it with the instance I am creating with the specific additional data (put extra). How can I achieve this?
Heres the code of my notification:
public void newMessageNotification(String title, String message, String otherusernumber, String requestStatus, String sendername) {
notificationManager = NotificationManagerCompat.from(this);
Intent chatIntent = new Intent(this,ChatActivity.class);
//these three strings define the behaviour of the chat activtity
chatIntent.putExtra("otherUsername", sendername);
chatIntent.putExtra("otherUsernumber", otherusernumber);
chatIntent.putExtra("requestStatus", requestStatus);
chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), chatIntent,
PendingIntent.FLAG_UPDATE_CURRENT);;
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_new_message)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
Try below code
Add PendingIntent in notification
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Full create notification code
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =
(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager . getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String Ch_id = "yourappname_01";
// The user-visible name of the channel.
CharSequence name = "Notification";
// The user-visible description of the channel.
//String description = getString(R.string.channel_description);
int importance = NotificationManager . IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(Ch_id, name, importance);
mChannel.setSound(defaultSoundUri, new AudioAttributes . Builder ().build());
notificationManager.createNotificationChannel(mChannel);
// Create a notification and set the notification channel.
notification = new Notification . Builder (this, Ch_id)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setContentIntent(pendingIntent) //Add PendingIntent
.setChannelId(Ch_id)
.build();
} else {
// Create a notification
notification = new Notification . Builder (this)
.setSmallIcon(R.drawable.notify)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(remoteMessage.getData().get("title"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent) //Add PendingIntent
.build();
}
//Generate Diff Notification
int m =(int)((new Date ().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m, notification);
Update
Intent intent = new Intent(this, OpenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
PendingIntent.FLAG_ONE_SHOT);
I hope this can help you!
Thank You.
Try to add flag: Intent.FLAG_ACTIVITY_NEW_TASK to your Intent
Try Below
Intent intent = new Intent(this, ActivityDestination.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//pendingIntent = PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ACTIVITY_NEW_TASK);
I have created a notification using a simple layout with textview and imageview. But after calling notificationManager.notify(...); does not show the notification.
Following is the code i have created
// Create notificationmanager instance
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_LOW;
// Setting the channelid if versioncode is greater than Oreo's
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationManager.createNotificationChannel(mChannel);
}
// Creating remote views for the notifications
RemoteViews notificationLayoutCollapsed = new RemoteViews(context.getPackageName(), R.layout.notification_layout_collapsed);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notification_layout_expanded);
// Passing the intent
Intent notificationIntent = new Intent(context, HomeActivity.class);-
// setting the flags
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Style MOST LIKELY ISSUE IS HERE
NotificationCompat.Style style = new androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle();
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.app_name))
.setContent(notificationLayoutExpanded)
.setCustomContentView(notificationLayoutCollapsed)
.setCustomBigContentView(notificationLayoutExpanded)
.setStyle(style)
.setAutoCancel(false)
.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID, mBuilder1.build());
I have also tried using the style = new NotificationCompat.DecoratedCustomViewStyle();
But when i set the style as BigText or BigPicture and do not set the customcontentview i do see the notification.
Edit
I have used both v4.NotificationCompat, androidx.NotificationCompat
Problem is in my code, I think some step is missing. Pls help.
I have tried below code for it with custom Notification but its not working. I want to display counter in my custom notification like music player .
final RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.layout_workout);
remoteViews.setTextViewText(R.id.layout_workout_tv_name, type + " " + videoList.get(position).getName());
remoteViews.setTextViewText(R.id.layout_workout_tv_time, strTime);
final Intent pauseWorkout = new Intent();
pauseWorkout.setAction(ACTION_PAUSE);
final PendingIntent pauseWorkoutPendingIntent = PendingIntent.getBroadcast(this, 101, pauseWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent resumeWorkout = new Intent();
resumeWorkout.setAction(ACTION_RESUME);
final PendingIntent resumeWorkoutPendingIntent = PendingIntent.getBroadcast(this, 102, resumeWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent skipWorkout = new Intent();
skipWorkout.setAction(ACTION_SKIP);
final PendingIntent skipExercisePendingIntent = PendingIntent.getBroadcast(this, 103, skipWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent completeSetWorkout = new Intent();
completeSetWorkout.setAction(ACTION_COMPLETE_SET);
final PendingIntent completeSetPendingIntent = PendingIntent.getBroadcast(this, 104, completeSetWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setAutoCancel(false)
.setContent(remoteViews)
.setShowWhen(false)
.setOngoing(true);
notificationBuilder.setWhen(System.currentTimeMillis());
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_complete_set, completeSetPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_pause, pauseWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_resume, resumeWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_skip, skipExercisePendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
I have also tried below code as well
.setPriority(Notification.PRIORITY_MAX)
For the higher priority notifications, use below code snippet...
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
How to create custom default notification? I am using Remote view to create the notification. but its not what was expect. Below is the image attched. I want to create the notification as that, with two button at bottom(like The Big Meeting is defined.
Any tutorial will be really helpful.
Below is code snippet what I have written.
Intent snoozeIntent = new Intent(LockableActivity.INTENT_SNOOZE);
PendingIntent pendingSnoozeIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
Intent gotItIntent = new Intent(LockableActivity.INTENT_GOT_IT);
PendingIntent pendingGotItIntent = PendingIntent.getBroadcast(context, 0, gotItIntent, 0);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.view_notification);
remoteView.setTextViewText(R.id.tv_title, entity.getClientName() + " appointment is late");
remoteView.setTextColor(R.id.tv_title, context.getResources().getColor(android.R.color.black));
remoteView.setOnClickPendingIntent(R.id.btn_snooze, pendingSnoozeIntent);
remoteView.setOnClickPendingIntent(R.id.btn_got_it, pendingGotItIntent);
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_content)))
.setContent(remoteView);
But here I have my xml. Is there other way by which I could DD THE BUTTONs at bottom.
You need to set your remote view to the bigContentView field in the notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true).build();
notification.bigContentView = remoteView;
mNotificationManager .notify(0,notification);