this is my source code. I need, to make it undeletable - it should be delete after click on that, not by swiping in the notification manager. Is it possible?
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("NotiClick", true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
Notification Noti;
Noti = new Notification.Builder(this)
.setContentTitle("Dont Lose It")
.setContentText("Stop monitoring by click!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, Noti);
}`
You need to use setOngoing(true) with your Notification.Builder:
Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).
Related
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());
I am using the onDestroy method, which I have learned is not always called when app is closed. And this has created a problem for me, where I would like to have the notification pop up every time the app closed. Is there a better way to do this? By the way, onStop and onPause are not options for me, because my app runs as a background service. Interestingly, my onDestroy method works everytime my background service is running, but whenever it is turned off, and just the app interface is open, my onDestroy never is called. This seems weird because I thought that onDestroy isn't called if the device is lacking resources, and running my service would be taking up more resources than not. Idk. Any help on this would be greatly appreciated!
`#Override
public void onDestroy(){
super.onDestroy();
Log.d("asdasd","asdasdasdasdasd");
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
}`
This method is not always called, so you should try from background service, as there's also some destruction process alert.
So please try to do what ever you want there in the services.
Please check out this ..
onDestroy() may or may not be called on any given activity or service. The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.
You don't. Your process may be terminated for any reason, at any time, by the user or by the OS. You may or may not be called with onDestroy() when that occurs. While you may be able to improve your success rate a little via onTaskRemoved() on a Service, this itself has proven unreliable, and it will not cover all scenarios.
you are doing it wrong way. In onDestroy method all other stuffs should implemented before super.onDestory()
try this
#Override
public void onDestroy(){
Log.d("asdasd","asdasdasdasdasd");
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
super.onDestroy();
}`
onTaskRemoved()
{
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
}
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()
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
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.