How can I get the title or the notification ID that I want to Clear.
My App produces several notification .
Api is 19 .
using  NotificationListenerService useful?
Yes, you can use getActiveNotifications from NotificationListenerService to get all active notifications.
StatusBarNotification[] activeNotifications = service.getActiveNotifications();
Then loop through available notifications and use getId()
if(activeNotifications!=null){
for(StatusBarNotification notification : activeNotifications ){
if (notification.getId() == ID_TO_CHECK) {
// do your operation
}
}
}
Related
I'm using OneSignal to send push notifications to iOS and Android devices. Is it possible to discard a push notification when it's received on a iOS and Android device? It's not all of them, but some based on the payload data and if the user has the device in the background or foreground.
Update:
"discard" meaning that it is never shown as a notification.
What are my options when I don't want to disturb users with updates if they are already in foreground using their app, but in background I like to show the notification?
In OneSignal you want to use setNotificationWillShowInForegroundHandler method to decide whether the need to show a notification to the user or not.
For Android
OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
#Override
void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
// Get custom additional data you sent with the notification
JSONObject data = notification.getAdditionalData();
if (/* some condition */ ) {
// Complete with a notification means it will show
notificationReceivedEvent.complete(notification);
}
else {
// Complete with null means don't show a notification
notificationReceivedEvent.complete(null);
}
}
});
For IOS
let notificationWillShowInForegroundBlock: OSNotificationWillShowInForegroundBlock = { notification, completion in
print("Received Notification: ", notification.notificationId ?? "no id")
print("launchURL: ", notification.launchURL ?? "no launch url")
print("content_available = \(notification.contentAvailable)")
if notification.notificationId == "example_silent_notif" {
// Complete with null means don't show a notification
completion(nil)
} else {
// Complete with a notification means it will show
completion(notification)
}
}
OneSignal.setNotificationWillShowInForegroundHandler(notificationWillShowInForegroundBlock)
Note: For more info, you can check the documentation.
https://documentation.onesignal.com/docs/sdk-notification-event-handlers#foreground-notification-received-event
When creating a bundled notification using setGroup() and setGroupSummary() I am having some strange issues regarding the behaviour of the notifications.
So, as a reference. This example contains the issue:
var isFirstNotificationInGroup = true
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.forEach {
if (it.notification.group == groupId) {
isFirstNotificationInGroup = false
}
}
}
val builder = NotificationCompat.Builder(this, channelId).apply {
color = resources.getColor(R.color.colorAccent)
priority = NotificationCompat.PRIORITY_MAX
setSmallIcon(R.drawable.ic_dotoo_logo)
setContentTitle(title)
setContentText(body)
setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
setAutoCancel(true)
setCategory(NotificationCompat.CATEGORY_SOCIAL)
setGroup(groupId)
setGroupSummary(isFirstNotificationInGroup)
}
< ... >
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
What happens?
The first notification will be shown as it should. So no issues here.
Then, when we show the second notification. It replaces the first one. This shouldn't happen. And no, it is not due to the notification ID. That's not related to this as far as I know.
But, when we show a third (or more) notification, the bundle works as expected and shows two (or more) bundled notifications. But the first one is... gone.
Thanks in advance for helping me.
I have fixed it by creating a seperate summary notification when isFirstNotificationInGroup is true.
This will be send just before the 'real' notification will be send.
I made an android app using fcm subscribed to topic.
And here is a part of my code in FirebaseMessagingService.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
mApplication = (ApplicationAVL)getApplication();
Log.d("FCM received === ", "ok");
cancelNotification();
ApplicationAVL mApplication = (ApplicationAVL) getApplication();
checkPollingService = isMyServiceRunning(AVLPollingService.class);
if(!mApplication.isPollingNow() || !checkPollingService){
EventBus.getDefault().post(new FcmToPollingEvent());
}
}
There is no codes for notification, but notification message always show also after closed app.
cancelNotification() is a method for remove notification I tried, but it doesn't work.
public void cancelNotification(){
String ns = NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager)getSystemService(ns);
nMgr.cancelAll();
}
UnsubscribeToTopic onDestory, but it doesn't work, neither.
How can I disable fcm notification ??
Please help me...
I am trying to understand you question. Based on the title of the question, the answer is to work on the message(payload) which comes from server or other source. put "silent":"true" in data message as the following example:
{
"data": {
"title": "afruz",
"body" : "hello!",
"silent":"true"
}
"registration_ids" : [""]
}
The push notification will invoke onMessageReceived without any message showing on top bar.
If you concern about unsubscribing topic from fcm, you can try to do it at application onPaused instead of onDestroid.
Hopefully, my answer is helpful to you.
I want to remove all cancelable notifications from status bar. I know how to remove my app notifications I have seen this post before, butI want to remove other apps notifications.
There is a "Clear" button in notifications in all android versions which clears all notifications with this flag: Notification.FLAG_AUTO_CANCEL
That means it's possible to cancel all notifications. But I haven't found any document about how to do that. Is there anybody guide me how to do that?
Thanks
Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.
First inside the onNotificationPosted method you store all the StatusBarNotification objects.
Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in onNotificationRemoved method.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}
Finally you can remove any notification using cancelNotification method.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}
To remove all notifications:
Kotlin
val NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
NM.cancelAll()
Java
NotificationManager NM = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE)
NM.cancelAll()
I have an Android app developed with Ionic Framework. I'm using the ngCordova plugin for push notifications and sending them using parse.com
The notifications are received when the app is running, but the notifications are not shown on the notification tray when the app is on background. I receive something like this:
notification = {
payload: {
data: {
alert: "message",
}
}
}
But, when I send them directly through CGM, the notification do appear on the notification tray. And the object I receive is like:
notification = {
message: "this appear on notification tray",
payload: {
message: "this appear on notification tray"
}
}
Is there something wrong with Parse? Or is something I'm missing about Parse?
This is kind of an old post, but I ran across this issue using Xamarin and Parse Push notifications, but my work around may work for you (and others that may see this in the future).
I wound up broadcasting a local Push Notification after the Parse notification is received.
First assign a receiver to the Parse notification event:
ParsePush.ParsePushNotificationReceived += PushNotificationReceived;
Then in the method:
void PushNotificationReceived (object sender, ParsePushNotificationEventArgs e)
{
var payload = JObject.Parse (e.StringPayload); // Parse the JSON payload
Notification.Builder builder = new Notification.Builder (this);
builder.SetContentTitle (payload ["alert"].ToString ());
builder.SetContentText (payload ["androidDetail"].ToString ()); // Note: this is another field I added to the Parse Notification
builder.SetDefaults (NotificationDefaults.Sound | NotificationDefaults.Vibrate);
builder.SetSound (RingtoneManager.GetDefaultUri (RingtoneType.Notification));
builder.SetSmallIcon (Resource.Drawable.small_notification_icon);
var largeIcon = BitmapFactory.DecodeResource (Resources, Resource.Drawable.large_notification_icon);
builder.SetLargeIcon (largeIcon);
var notification = builder.Build ();
notification.Defaults |= NotificationDefaults.Vibrate;
NotificationManager notManager = (NotificationManager)GetSystemService (Context.NotificationService);
notManager.Notify (0, notification);
}
Hope this helps you and anyone else who comes across this!