How to update notification title and text without sending new notification? [duplicate] - android

This question already has answers here:
Update text of notification, not entire notification
(3 answers)
Closed 5 years ago.
I have an app in which i have to update notification text and title without sending new notification. How do i do that i have tried below mentioned code.
code:-
notifica("Test", "Testinggggggggggg");
m_send = (Button) findViewById(R.id.send_another);
m_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
builder.setContentTitle("Test3");
builder.setContentText("Test3333333333");
notificationManager.notify(0, builder.build());
}
});
}
public void notifica(String title, String message) {
int color = getResources().getColor(R.color.colorPrimary);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, HomeScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setAutoCancel(false);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setColor(color);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}

use this to update the title with same notification_id as you are using 0 for now
builder.setContentTitle(title);
notificationManager.notify(0, builder.build());

From Android Developer
notify
added in API level 1
void notify (int id, Notification notification)
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Using same notification id you can update notification text using local broadcast manager.

Related

Sending notification on post added

I am creating a news android app and i want to send a notification when post added to database.i know Google Firebase Console but i want to send notification automatically.i have following codes:
public void sendNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setPriority(Notification.PRIORITY_MAX);
Intent intent = new Intent(MainActivity.this, NewsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("My Company");
mBuilder.setContentText("New news received.");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
And using this in Apiservice:
apiService.getNews(new ApiService.OnNewsReceived() {
#Override
public void onReceived(List<News> news) {
sendNotification();
adsesAdapter = new AdsesAdapter(NewsActivity.this, ) );
recyclerView.setAdapter(adsesAdapter);
}
});
This codes just show notification when user is on app... Is there any way to show notification on news received without opening app???

Update notifcation text

I have currently setup a small application which sets an alarm on the device and stores relevant information inside of an SQL database e.g. Alarm name and time. When the alarm is activated my application sends a notification to the user. Currently each notification is static which means every message is the same. I would like to now allow my application to grab the name of the alarm which has been activated and display it in the notification. Now I know that when setting multiple alarms you require multiple ID's which I have stored in side of my SQL and I'm guess the same when it comes to sending a notification. Is there a way of matching my Alarms ID with one on a notification so that it knows what message to send e.g. alarm name?
Current code for setting my Alarm
pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
My Broadcast receiver...
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Standard text")
.setContentText("Standard text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
}
//////////////
Update on my situation which is slowly driving me crazy
I can now set the text from my SQL to my notification but its only the first from my database each time. Now I've come up with two possible solutions which are only theory's.
Is there a way in which each time the database is called to the notification it will move onto the next result?
As I mentioned above I'm setting an alarm and when the alarm goes off it then calls the notification. Now In my pending intent for the alarm I'm giving it a id which you can see as alarm_request_code is there a way of giving it to my notification ID and then setting up a statement where if the notification ID is equal to or in the list of my alarm ID's stored on my SQL then it will search for the correct text to input.
MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context, null, null, 1);
Cursor cursor = myDatabaseHandler.getAllProducts();
// Information we are trying to acquire but can only get first result
String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));
// Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));
////
Log.i("The reciever is working", "perfect");
//create an intent to service
Intent service_Intent = new Intent(context, MessageService.class);
context.startService(service_Intent);
//Notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle(name)
.setContentText(date)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());
}
}
To update text store mBuilder variable
and update later in this way:
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
EDIT
private void simple() {
Intent intent = new Intent(context, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setContentIntent(pIntent);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
} else {
mBuilder.setSmallIcon(someiconLollipop);
mBuilder.setColor(somecolorLollipop]);
}
noti = mBuilder.build();
//use flags if you need:
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
to update the text:
private void showText() {
String head = yournewhead;
String body = yournewbody;
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
Per the docs you can do the following:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
The trick here is your notify id, which must be the same in order to behave appropriately, in your case 0.
So as long it has not been dismissed it should work accordingly. I will suggest having a unique id as a constant for your custom notification such as 123.
notificationManager.notify(0, builder.build());

Notification not showing up in android 19

I am trying to notify using notification as below: The main activity has the following code, but nothing is showing up.
mEmailSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Enable Call Permission")
.setContentText("Please Enable or Accept call permission");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(MainActivity.this, PermissionActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
save();
}
});
It seems it doesnt work without an icon.
.setSmallIcon(R.drawable.small_icon);
see this post:
Notification Not showing

How to handle notification count when app is in background in firebase [duplicate]

This question already has answers here:
Push notification works incorrectly when app is on background or not running
(3 answers)
Closed 6 years ago.
I have successfully handled notifications when app is in foreground by FirebaseMessagingService class. which will generate notification from app using this code:
private void sendNotification(String type, String typeID, String messageBody, String title) {
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
try {
count = MazkaraPref.getInstance().getInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, 0);
count++;
Log.e("count", ":" + count);
MazkaraPref.getInstance().saveInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, count);
} catch (Exception e) {
e.printStackTrace();
}
}
This way I have handled Notification count when application is running in foreground. But when app is in backgorund we have no control to generate notification or count notification. So if i want to know how many notifications are pending in status bar or how many notifications are arrived at the time of SplashScreen open. How can i achieve this Notification count.I have tried code above but it works if app is running only. I am not getting it how to get pending notification count.
You can calculate the count in onMessageReceived method of FirebaseMessagingService, It will get called even when app is in background.

autohide the notification when clicked on the buttons in notification in android

I have tried setAutocancel as well as FLAG_AUTO_CANCEL but none of these work and i have two buttons inside notification which open two diff activities.. kindly help me
public void createNotification(View view) {
Intent yesIntent = new Intent(this, MyDialog.class);
yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
yesIntent.putExtra("ACTION", 1);
PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);
Intent noIntent = new Intent(this, MyDialog.class);
noIntent.putExtra("ACTION", 0);
PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder (this)
.setContentTitle("New Project Approval")
.setContentText("Project Description")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.bell)
.addAction(R.mipmap.approve_ic, "Approve", yespIntent)
.addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, noti.build());
}
You have to cancel your notification manually in your activity. Pass the notification ID to your PendingIntent to retrieve it in your Activity and cancel the corresponding notification via the NotificationManager like :
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);
A good and full example : How to dismiss notification after action has been clicked
Previously I callied the NotificationManager outside the onCreate() method, which wasn't working. But following the StackOverflow threadr suggested by #bubu I resolved it by calling NotificationManager inside onCreate();

Categories

Resources