I'm want to remove the "system" screenshot notification. I already achieved it, using the Notification Listener Service and the ID which I got from my phone. But I found out that the ID for the system notification is changing on some samsung and other devices. How can I make sure to remove the notification also on other devices. is there a different way to detect that the system screenshot notification has shown? Some specific TAG?
Edit: This how I do it right now:
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
//super.onNotificationPosted(sbn);
Log.i(TAG, "ID: " + sbn.getId());
Log.i(TAG, "Package: " + sbn.getPackageName());
//Remove Notification
if (sbn.getId() == 2131820569) {
if(status.getBoolean("comboserviceaktiv",false) || status.getBoolean("serviceaktiv",false) && standardprefs.getBoolean("settings_silent",false)){
if(Build.VERSION.SDK_INT >= 21) {
this.cancelNotification(sbn.getKey());
Log.i(TAG, "System Screenshot Notification deleted");
} else {
this.cancelNotification(sbn.getPackageName(),sbn.getTag(),sbn.getId());
Log.i(TAG, "System Sreenshot Notification deleted");
}
}
}
}
Thank you very much!
Related
I am using a notification listener to snooze unwanted notifications, but how do I un-snooze these once my app is no longer running? Currently they only way I have found to do this is the user must reboot their phone.
This is how I am snoozing the notification, I found out I can use getSnoozedNotifications to get a list of snoozed apps, but what is the command to un-snooze it?
snoozeNotification(sbn.getKey(), Long.MAX_VALUE - System.currentTimeMillis());
And this is how I get the snoozed apps, I just can't find anywhere that tells me the command to un-snooze it, I have even tried to set the duration to 0.
private void clearSnoozedNotifications()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
StatusBarNotification sbns[] = getSnoozedNotifications();
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.e("Notification Listener", "sbn is null");
return;
}
Log.e("Notification Listener", "Starting: " + sbn.getKey());
snoozeNotification(sbn.getKey(), 0);
} catch (Exception e) {
Log.e("Notification Listener", "error: " + e.getMessage());
}
}
}
}
I have observed on Android 11 that you can restore the notification by calling snoozeNotification on the same key with a low duration number greater than zero.
For example I use the following:
snoozeNotification(key, 100L);
On earlier versions of Android the notifications will re-appear after a reboot when they have been snoozed, in Android 11 they won't.
I am trying to parse the Whatsapp notifications that i receive on my device using NotificationListenerService.
On receiving a notification for the first time, i am able to retrieve the Text message. However, when the second message arrives, i get the message: "2 new messages". So basically here the messages in the notification are getting stacked.
Below is the code i have used:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
String key = sbn.getKey();
sbn.getNotification().tickerText + "\n Package Name:" + sbn.getPackageName());
Log.i(TAG,"getTAG:"+sbn.getTag());
Log.i(TAG,"getKey:"+sbn.getKey());
Log.i(TAG,"getGrpKey:"+sbn.getGroupKey());*/
Bundle bundle = sbn.getNotification().extras;
String title = bundle.getString("android.title");
String text = bundle.getCharSequence("android.text").toString();//This gives me the actual message, the first time the notification arrives
Log.i(TAG,"Bundle is:"+bundle);
Log.i(TAG,"Title:"+title);
Log.i(TAG,"Text:"+text);
NLService.this.cancelNotification(key);
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG,"********** onNOtificationRemoved");
Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
}
}
So my query here is, how do i parse/fetch messages from this notification? Or is there another way to fetch incoming whatsapp messages?
One approach that i can think of is marking the message as "Read", but has anyone able to mark a whatsapp message as "read" programmatically?
I have read most of the older posts on Stackoverflow and also whatsapp official site provides us a way of send a message.
Any sort of suggestions shall be really helpful.
I'm fighting against the NotificationListenerService without much luck.
I tried a lot of recipes found here and there, especially on so... But it works quite inconsistently.
A first look at the code :
Manifest :
<service
android:name=".services.NLService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Then the service itself :
public class NLService extends NotificationListenerService {
private String TAG = "NLService";
// bind and unbind seems to make it work with Android 6...
// but is never called with Android 4.4...
#Override
public IBinder onBind(Intent mIntent) {
IBinder mIBinder = super.onBind(mIntent);
Log.i(TAG, "onBind");
return mIBinder;
}
#Override
public boolean onUnbind(Intent mIntent) {
boolean mOnUnbind = super.onUnbind(mIntent);
Log.i(TAG, "onUnbind");
isNotificationAccessEnabled = false;
try {
} catch (Exception e) {
Log.e(TAG, "Error during unbind", e);
}
return mOnUnbind;
}
// onCreate is called with Android 4.4
// because the service is explicitly started from the MainActivity.
// Not on Android 6 where the system binds this service itself...
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "********** onCreate");
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG, "********** onNotificationPosted");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "********** onNOtificationRemoved");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
}
}
And in the main Activity the service is started or we ask the user to enable the setting if needed :
if (!Utilities.hasNotificationAccess(this))
{
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
Log.i(TAG,"hasNotificationAccess NO");
}
else
{
Log.i(TAG,"hasNotificationAccess YES");
// without this startService, it never works with Android 4.4...
// but this is not needed in Android 6...
Intent mServiceIntent = new Intent(this, NLService.class);
startService(mServiceIntent);
}
Obviously, the secure setting access for notifications is enabled...
On Android 6 :
On the NLService itself, the addition of the onBind and onUnbind methods make it work, I can se the onBind log, and the onNotificationPosted is triggered nicely. But it lasts until the next launch of the app, then no more binding, no more onNotificationPosted, just nothing happens until I go back to security settings to uncheck-recheck the notifications access: repeating this each time the App is started is not something possible in a production environment.
On Android 4.4 :
In contrast to Android 6, in the MainActivity I need to explicitly start the NLService otherwise it never stats by itself. But once again it works for the first launch and never works again until uncheck-recheck the security setting...
Did I miss something obvious ?
There is a problem with Android caching. When you upload app on the device, OS connects your service to Notification Manager. If you ran your app before, Android founds this service in cache so it's not reconnected.
Workaround: before pushing the app on your device, rename your service (refactor option in Android Studio works well) - Android will recognize this service as new one and connect to Notification Manager.
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - this method will be invoked after connecting to Notification Manager so you can check if your service is connected or not.
Does anyone know how can I check if there are any notifications programmatically in android?
I want to check if there is any notification currently available in the notifications list.
for example, if there is any notification, the LED light is turned on and if the notifications list is cleared, the LED light is turned off.
I just want to know the condition or the code that allow us to check if there are any notifications available in the notifications list.
if ( condition - there are any notifications )
// my code
You can use NotificationListener API, which is available on Android 4.3+. To do that you just simply need to create a simple Service that extends NotificationListenerService.
Here is some sample code
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG, "Notification posted");
Log.i(TAG, "ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "Notification Removed");
Log.i(TAG, "ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
}
}
A complete tutorial is available here
Prior to this version of Android, you can make a hack through the AccessibilityService as described here
How to change the notification message in Android to localised it?
I just want to change the message what I get from Urban AirShip before it will be display on notification bar?
You can using this method: PushManager.shared().setNotificationBuilder(null); and send your own notification with android notification from the SDK
Yes you can modify the message once you receive the message from Urban AirShip for your internal use in the app but can't show the modified message in the notification bar.
You can check the message in your IntentReceiver
public class IntentReceiver extends BroadcastReceiver {
private static final String logTag = "Hey";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(logTag, "Received intent: " + intent.toString());
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
Log.v(logTag, "Received push notification. Alert: " + intent.getStringExtra(PushManager.EXTRA_ALERT)
+ ". Payload: " + intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA) + ". NotificationID="+id);
//can get your message here
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
Log.v(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT)+ ".Payload:" + intent.getStringExtra("PushManager.EXTRA_STRING_EXTRA"));
Intent launch = new Intent(Intent.ACTION_MAIN);
UAirship.shared().getApplicationContext().startActivity(launch);
} else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
+ ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
}
}
}
I found the solution, its kind a dirty one, but right know I need to use this.
Just after get notification from Urban AirShip I cancel all and send my own with changed message.
I read the answers and came up with a combined solution.
You want to disable UrbanAirship's default notification handler. If you do that, it won't generate and show you the notifications at all.
PushManager.shared().setNotificationBuilder(null); (Using David T's suggestion)
You want to build your own notification. This can be done inside your IntentReceiver. This link will do the trick.
Hope this helps others.
Please using that code to disable Urban Airship's notifications, override BasicPushNotificationBuilder:
BasicPushNotificationBuilder nb = new BasicPushNotificationBuilder() {
#Override
public Notification buildNotification(String alert,
Map<String, String> extras) {
return null;
}
};
// Disable notifications
PushManager.shared().setNotificationBuilder(nb);