how to add notification in list view on android - java

I made an application organizer reminders. The application has system work of the date and time as a reminder media. I mean how how to give a notification to the listview, if the agenda has been implemented? Please help.
thank you

If I understood you correctly, you want your app to display notifications, right? To do so, try code from this tutorial
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
If that's not what you need, please clarify your question.

Related

Create Android java notification for Flutter package and redirect to the app when clicked

I'm creating a flutter package. I am creating a notification in the Android java section, but when I click on this notification, I want to go to the application, but I can't figure out how to do it.
My notification generation code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL)
.setSmallIcon(R.drawable.ic_notification)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(publicVersion ? NotificationCompat.VISIBILITY_PUBLIC
: NotificationCompat.VISIBILITY_PRIVATE);
builder.setContentTitle(getString(s));
return builder.build();
You can use pending intent : https://developer.android.com/training/notify-user/navigation#java
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);

Invoking a method without launching the UI

I'd like to have a notification with a few buttons that, when tapped:
would invoke a method, but without launching any UI (Activity);
would not cause the notification bar to collapse.
I've created a notification (using NotificationCompat.Builder) with an action as shown below.
Tapping the action button launches the MainActivity UI and also collapses the notification bar.
Any hints on how to achieve the desired behavior described above?
My code:
Intent actionIntent = new Intent(this, MainActivity.class);
actionIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
actionIntent.setAction(MY_ACTION_ID);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setVisibility(VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(false)
.addAction(new NotificationCompat.Action(
0,
"My action",
PendingIntent.getActivity(this, 0, actionIntent, 0)));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(MY_NOTIFICATION_ID, builder.build());
Instead of starting an Activity with the PendingIntent, you can start a Service or a BroadcastReceiver. Look at the documentation for both of these at https://d.android.com.

Restart the application when click on notification

I am using following code to open my application when clicking on Notification
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.cancel(Constants.PUSH_ID);
mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
The problem is, if the application is already running, it opens startup application after my current activity.
Intent x = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
How can i close the old and start the application new one.
OK, i found a very cool and simple solution. I would just pass an extra boolean to my intent like i.putExtra("shouldRestart", true);
on my SplashActivity, i check
if(getIntent().getBooleanExtra("shouldRestart", false))
{
// we need to clear all activities from top
finishAffinity();
}

Putting an View object in notification android

I am trying to put a View object (such as TextView, Edittext, Button, etc) in a notification in Android. I am new to this assignment and I am confused so here is my attempt so far.
So I have a notification to appear as following:
Intent intent = new Intent(context, Receiver.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Message From")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "", pIntent).build();
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(context.NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
I know this can be done for Jelly Bean and after version. I was wondering if its possible to insert a View object possibly through PendingIntent using Intent?
If anyone can guide me in the right direction that would be great
see these
http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/
Create custom notification, android

Notification opening Google play

I would like make a notification when clicking open Google Play.
I'm using a google Api example.
Another question is, the notification Id can be static one?
Is neceesary add some other stuff in androidmanifest?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
Notification ID is only for Notification identification and also if the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.
You must add your intent ResultActivity into your manifest.xml file and also if you set Notification sound then you must add Vibrate permission into your manifest.xml file that's it.

Categories

Resources