How to make notification dissapear after click - java

I want to make a notification disappear after a click, I don't want to go to new Activity. This is how I build a notification :
public static void getSynchronizeNotification(Context context, DataResponse dataResponse){
Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);
Intent in = new Intent(String.valueOf(context));
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, in, 0);
Notification notification = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setContentText("Zmiany po synchronizacji...")
.setStyle(new Notification.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
.setLargeIcon(Largeicon)
.setAutoCancel(true)
.setVibrate(vibratePattern)
.setSound(alarmSound)
.setPriority(Notification.PRIORITY_MAX)
.build();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_SYNCHRONIZE_ID, notification);
}

I change this :
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, in, 0);
to
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
and it works

Related

set different colors for notification actions

Hi I am setting a notification for incoming call with two actions : Answer and Decline . I need to set Green color for Answer action and red for Decline . But i couldnt find a solution.
Here is my code :
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,"Call")
.setSmallIcon(R.drawable.ic_stat_rider_logo)
.setContentIntent(contentIntent)
.setContentTitle(generalFunc.retrieveLangLBl("","LBL_SINCH_NOTIFICATION_CONTENT"))
.setContentText(call.getHeaders().get("Name") +" "+ generalFunc.retrieveLangLBl("","LBL_SINCH_NOTIFICATION_TITLE"));
builder.addAction(getServiceNotificationAction(mContext, denyCallIntent(mContext,call),R.drawable.ic_call_icon, R.string.decline));
builder.addAction(getServiceNotificationAction(mContext, answerCallIntent(mContext,call),R.drawable.com_facebook_close, R.string.answer));
if (callActivityRestricted()) {
builder.setFullScreenIntent(contentIntent, true);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setCategory(NotificationCompat.CATEGORY_CALL);
}
private NotificationCompat.Action getServiceNotificationAction(Context context, Intent intent, int iconResId, int titleResId) {
PendingIntent pendingIntent = Build.VERSION.SDK_INT >= 26 ? PendingIntent.getForegroundService(context, 0, intent, 0)
: PendingIntent.getService(context, 0, intent, 0);
return new NotificationCompat.Action(iconResId, context.getString(titleResId), pendingIntent);
}
I tried setColor() , but it sets unique color for both actions.
Please help me to solve this . thanks in advance
I have tried your code and achieved it with the help of Spannable class.
public void sendOnChannel1(View v) {
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, activityIntent, 0);
Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
broadcastIntent.putExtra("toastMessage", message);
PendingIntent actionIntent = PendingIntent.getBroadcast(this,
0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setColorized(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.mipmap.ic_launcher, getMyActionText(R.string.answer,R.color.green), actionIntent)
.addAction(R.mipmap.ic_launcher, getMyActionText(R.string.decline,R.color.red), actionIntent)
.build();
notificationManager.notify(1, notification);
}
Here is a helping method which i used:
private Spannable getMyActionText(#StringRes int stringRes, #ColorRes int colorRes) {
Spannable spannable = new SpannableString(this.getText(stringRes));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
spannable.setSpan(
new ForegroundColorSpan(this.getColor(colorRes)), 0, spannable.length(), 0);
}
return spannable;
}

How can I know which button was clicked in the notification?

I am developing an application, I have 2 buttons in an on the notification. How can I know which button the user has clicked?
This my Notification codes;
public void NotificationSettings(Context context){
Intent stateIntent = new Intent(context, MyBroadcastReceiver.class);
stateIntent.putExtra("id", 100);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, stateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "access2020")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent);
notificationManager = NotificationManagerCompat.from(context);
}
and My Broadcast
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
int notificationId = intent.getIntExtra("id", 0);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notificationId);
}
You should pass an identifier to the intent as an extra and then retrieve in your BroadcastReceiver.
public void NotificationSettings(Context context){
// put an extra identifier for Set Active Action
Intent setActiveStateIntent = new Intent(context, MyBroadcastReceiver.class);
setActiveStateIntent.putExtra("id", 100);
setActiveStateIntent.putExtra("action", "Action.SetActive");
PendingIntent setActivePendingIntent =
PendingIntent.getBroadcast(context, 0, setActiveStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// put an extra identifier for Dismiss
Intent dismissStateIntent = new Intent(context, MyBroadcastReceiver.class);
dismissStateIntent.putExtra("id", 100);
dismissStateIntent.putExtra("action", "Action.Dismiss");
PendingIntent dismissPendingIntent =
PendingIntent.getBroadcast(context, 0, dismissStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "access2020")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", setActivePendingIntent)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", dismissPendingIntent);
notificationManager = NotificationManagerCompat.from(context);
}
Then in your BroadcastReceiver you can do the following:
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("action").equals("Action.Dismiss")) {
// perform your dismiss action
} else if (intent.getStringExtra("action").equals("Action.SetActive")) {
// perform your set active logic
} else {
// handle invalid action
}
}
I solved with this way but still i am not sure it is the right way
public void NotificationSettings(Context context){
Intent stateIntent0 = new Intent(context, MyBroadcastReceiver.class);
Intent stateIntent1 = new Intent(context, MyBroadcastReceiver.class);
stateIntent0.putExtra("id", 100);
stateIntent1.putExtra("id", 200);
PendingIntent pendingIntent0 =
PendingIntent.getBroadcast(context, 0, stateIntent0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 =
PendingIntent.getBroadcast(context, 1, stateIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "lemubitA")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Lemubit Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent0)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent1);
notificationManager = NotificationManagerCompat.from(context);
}
AND this my Broadcast
int notificationId = intent.getIntExtra("id", 0);
Toast.makeText(context, notificationId+"", Toast.LENGTH_SHORT).show();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notificationId);

Start an Activity when clicking a notification

I'm trying to start an activity by clicking on a notification but that only works when I'm already in the app.
Here's the code I think has to do with it:
private void sendNotification(String body) {
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
The following works in my app:
private void sendReminderNotification(Context context) {
Resources resources = context.getResources();
Intent notificationIntent = new Intent(context, TitleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.my_diary_launcher_icon))
.setContentTitle("MyDiary")
.setContentText("Reminder to input entry.")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
Use pendingIntent to pass data of intent:
private void sendReminderNotification(Context context) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound);
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

Notifications Code in Android

I am trying to write a simple notification code in a button that create a notification when i press on it but the notification doesn't appear when i press on the button here is the code i am using
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(SessionsActivity.this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(SessionsActivity.this)
.setContentTitle("hii")
.setContentText("hii2")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =NotificationManagerCompat.from(SessionsActivity.this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

make notification clickable and undestroyable

I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.
To keep the Notification, in that way the user can't click it away in any way, add this flag
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
To start an Activity you have to use this flag for your Intent
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Do like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
You want a foreground service as detailed here
It's worth noting that only android 4.3 added the persistent notification you seem to be after and it can be overriden by the users settings.
try using like this
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
in MainActivity.java
Dont not consume your notification; ie instead of calling below 2 line code in oncreate() call under onDestro()
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(mId);
You can use NotificationCompat.Builder's .setOngoing(true), use it like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"

Categories