How to make persistent notification? - android

I'm making an app that communicates to an Aurdino through bluetooth. It uses a service for the communication part. So as long as the service is running, I want a notification to be shown by the app. The user shouldn't be able to swipe the notification to make it go away.
Here's my notification function so far:
void showNotification(String title, String contentText)
{
...
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "default")
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(title) // title for notification
.setContentText(content)
.setAutoCancel(false)
.setOngoing(true);
mNotificationManager.notify(0, mBuilder.build());
}
Notice that I've called the setOngoing(true) but still I can swipe the notification from the notification drawer and it goes away when I do so.
How do I prevent the user from being able to swipe and cancel the notification? My app will automatically cancel it when it is terminated, in the OnDestroy() of the service.

You need to make a foreground service, while your service is active its notification too
So your service becomes so hard to be killed by the system
https://www.dev2qa.com/android-foreground-service-example/

Related

notification deleteIntent doesn't work when app close

a create push notification with next code:
NotificationCompat.Builder b = new NotificationCompat.Builder(context)
.setSmallIcon(smallIcon)
.setTicker(getRewardStr)
.setAutoCancel(false)
.setContentIntent(clickPendingIntent)
.setDeleteIntent(cancelPendingIntent)
.setContentTitle(title)
.setContentText(description)
.addAction(rewardAction)
.setStyle(bigTextStyle)
.setLargeIcon(largeIconBitmap);
in general - everything works ok, i receive all events via broadcast
but I just mention that if notification shows when app is open and than i swipe app from recent, than my notification is canceled but i didn't receive cancelPendingIntent.
is this some kind of default behaviour?
is there any way to catch notification cancel in this situation?

How to control frequency of notification update on locked screen

I have an application which updates a notification through a service.
This notification is updated every second. It shows a timer.
Currently on nougat, with device locked, notification update triggers screen to wake up, to show the updated notification.
I would like to control this as my notification is really frequent.
I would also like to avoid changing the update frequency of the notification. Doing so shows less accurate information to the user.
So what I am looking for is a programmatic way to control wake up screen frequency upon notification updates.
thank you :)
This below notification can be used to update the user frequently and also the screen will not wake up when locked up. you may try
if(notificationBuilder == null) {
notificationBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.extend(wearableExtender)
.setAutoCancel(true)
.setContentTitle("Random Notification")
.setContentText("Appears!")
.setOnlyAlertOnce(true)
.setOngoing(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(contentIntent);
}else{
notificationBuilder.setContentText("change this to current updated text");
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(888, notificationBuilder.build());

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop

NotificationManager.cancel() doesn't work: Notification isn't removed

I've been trying to remove a persistent Notification set by a Service using:
startForeground(1337, notification);
The code I'm using to cancel it:
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(1337); // cancel existing service notification, doesn't take effect
nManager.cancelAll(); //surpluous, but also doesn't take effect
To clarify why I am doing this: the Service starts with a default persistent Notification. When my app runs, it needs to replace this Notification with another. Using notify() on the existing Notification works perfectly, however, I need it to show the ticker text for the new Notification as well. This is why I decided to remove the existing Notification (using the code above), create a new one, and then I call startForeground() again and pass the new Notification to it, so my Service persists.
The problem is that you're issuing the Notification in an indirect way by using startForeground(). You can't just cancel that Notification for the same reason the system insists on you providing a Notification when starting a foreground Service. As long as your foreground Service is running, that Notification will be there.
In most cases, Services really shouldn't be in the foreground. If you can use a normal priority for your Service, then you can start and stop your Notification normally.
If you're actually doing something that truly does require a foreground Service, and if you really want to show the user a ticker text, I believe your only option is to issue another Notification.
You can always remove notification from a foreground service by callng stopForeground(boolean removeNotification). Then a service exits his foregroundState and once again can be killed by the system when the memory is needed.
You could update the notification by passing in an empty Builder.
if(showNotification){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_SECRET)
.setSmallIcon(R.mipmap.ic_spotify_white_24dp)
.setTicker("Playing Now")
.setContentTitle("Spotify")
.setContentText("Preview");
return mBuilder;
}else{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
return mBuilder;
}

Programmatically ending an ongoing notification - Android

I am developing a GPS-based app and have just started adding in my UX features such as notifications and progress bars but I'm stuck on using an ongoing notification.
As it is a GPS app, when user tracking is started, I set up an ongoing notification to show that they are being tracked but how do I stop this notification when they tap "stop tracking" in my app? Do I have to tell the NotifyManager something? I'm basically trying to get the functionality that music players have, as in the "playing" notification appears when the user presses play, but when they pause, that ongoing "playing" notification is destroyed.
Also, I've never worked with GPS before but should I be going about this in a Service so that the user won't stop being tracked if my app is taken out of memory by the OS? Or would that not happen?
I haven't done much with notifications but you might try this:
NotificationManager.cancel(id); // Where 'id' is the id of your notification
Replacing NotificationManager with the name of your instance of it, of course.
docs: http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28int%29
Or this:
Notification.Builder.setAutoCancel(true);
http://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel%28boolean%29
Start
int id = 01;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.service_header))
.setContentText("Connect to: http://" + httpd.getip() + ":8080")
.setContentIntent(pendingIntent)
.setOngoing(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(01, mBuilder.build());
Cancel
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);

Categories

Resources