Invoking a method without launching the UI - java

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.

Related

Activity won't launch from notification when the app is started from notification

I have an app that sends notifications, and when clicked, opens an activity from my app. If you launch the app and keep the app open and click notifications, it works properly. However, when the app is closed, it doesn't. This is what happens:
Close app > receive notification > click notification > opens the correct activity > receive another notification > nothing happens when clicked
The intended functionality is for the notification to open the activity regardless if it is already open, and just add it onto the task stack. So if you clicked 3 notifications, the stack would be ActivityA > ActivityA > ActivityA. Here is the code for the notification:
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(PendingIntent.getActivity(context, 1,
new Intent(context, ActivityA.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE)
)
.build());
I also tried using a broadcast PendingIntent. The BroadcastReceiver would be called but the call to Context#startActivity did nothing.
Intent intent = new Intent(YourActivity.class, NewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
PendingIntent
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(pendingIntent)
.build());

How to create a chronometer in a notification Android?

I have the following method below:
public NotificationCompat.Builder createNotification(Context context) {
Intent intent = new Intent(this, MapsActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
boolean running = true;
builder = new NotificationCompat.Builder(context)
.setContentText("conteúdo")
.setContentTitle("titulo")
.setSmallIcon(R.drawable.ic_today_black_24dp)
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setOngoing(running)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, MapsActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
.addAction(running ? R.drawable.ic_stop_black_24dp
: R.drawable.ic_play_arrow_black_24dp,
running ? "Pause"
: "play",
pIntent)
.addAction(R.drawable.ic_stop_black_24dp, "Stop",
pIntent);
notificationManager.notify(0, builder.build());
return builder;
}
In which launched a notification in the status bar, as shown below in the first notification:
To link the notification I do this:
NotificationCompat.Builder notification = createNotification(this);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification.build());
I would like to create a Chronometer in a notification, exactly as it appears in the Strava notification (second notification of the image), as shown above.
How to create a Chronometer in a notification?
So creating the App Widget layout is simple if you know how to work with Layouts. However, you have to be aware that App Widget layouts are based on RemoteViews, which do not support every kind of layout or view widget. Anyway if you need asistance with App Widget layout here is some guaidance:
https://developer.android.com/guide/practices/ui_guidelines/widget_design.html
Luckily for you RemoteViews support Chronometer as you can see from developer webiste: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
A think you can acces your Chronometer as usually and you could do something like this depends do you want it to pause, resume, whatever:
remoteView.setChronometer(R.id.myChronometere, SystemClock.elapsedRealtime(),
null, false); //pausing
You can use built in chronometer:
builder = new NotificationCompat.Builder(context)
builder.setUsesChronometer(true)
...
builder.build()

how to add notification in list view on android

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.

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