How to detect a notification in android? - android

I am making some animation in android and I want to pause it whenever notification pops up in screen. For example sms, message from messenger, whatsApp, viber etc.
I don't need to know what is the type of notification or handle it somehow. I just need to know when it pops up so I can call my pause() method. How I can achieve this?

What you need is a notification listener service.
public class NotificationService extends NotificationListenerService {
Context context;
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Send a braoadcast with some data
Intent notificationMessage = new Intent("SomeData");
notificationMessage.putExtra("Hello", "World");
LocalBroadcastManager.getInstance(context).sendBroadcast(notificationMessage);
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}
You can recieve the Local broadcast in you activity as follows
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("Hello");
// Do something with the data
}
}, new IntentFilter("SomeData"));
}
}
Also register the service in manifest
<service
android:name=".NotificationService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
you can find the official documentation here https://developer.android.com/reference/android/service/notification/NotificationListenerService
You can also find detailed demo here http://www.androiddevelopersolutions.com/2015/05/android-read-status-bar-notification.html

Related

How to send BroadCast in Oreo?

This is my class where am getting Notification from server->
public class GCMListener_Service extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
Intent notificationReceived = new Intent("com.example.mypackage”);
notificationReceived.putExtra(“key", fromWhere);
sendBroadcast(notificationReceived);
}
}
this i have declared in AndroidManifiestfile:
<service
android:name="com.example.pushnotification.GCMListener_Service"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
here i am trying to get Notification :
public class PushMessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Timber.d("Received push notification”);
}
}
here is my receiver in AndroidManifiest file->
<receiver
android:name=".notification.PushMessageReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.mobile.pushnotification.notificationReceived" />
</intent-filter>
</receiver>
using given code below android 8 (oreo ) it working fine i am getting and receive notification message in PushMessageReceiver class in onReceive method but in android i am unable to get notification unable to send broadcast can any one please suggest me how to send broad cast in Android O .
In your Application class register and unRegister class like this:
public class YourProjectApplication extends MultiDexApplication {
private static YourProjectApplication sInstance;
public static YourProjectApplication getInstance() {
return sInstance;
}
#Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public void registerInternetConnectivityBroadCast(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(appInternetStatus, intentFilter);
}
public void unSetConnectivityListener(AppInternetStatus.InternetConnectivityReceiverListener listener) {
try {
unregisterReceiver(appInternetStatus);
}catch (Exception e){
e.printStackTrace();
}
try {
AppInternetStatus.internetConnectivityReceiverListener = listener;
}catch (Exception e){
e.printStackTrace();
}
}
And then in your mainActivity class (which can also be your base class) call your broadcast receiver like this:
getInstance().registerBroadCast();
getInstance().setConnectivityListener(this);
And in order to unregister your notification. In your onPause() method of your main activity class :
#Override
protected void onPause() {
super.onPause();
getInstance().unSetConnectivityListener(null);
}

onNotificationPosted never called

I'm trying to implement a feature which would read the notification and it seems that I've a bug in it because the onNotificationPosted() of the NotificationListenerService is never called. I configured the manifest, modified the security settings properly and even started the service manually but it did nothing. Can it be that testing it in an emulator is not the best way to proceed.
Here is the Notification getter class:
/**
* Created by laurentmeyer on 28/02/15.
*/
public class NotificationListener extends NotificationListenerService {
public NotificationListener(){
Log.d("Notification", "Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification mNotification = sbn.getNotification();
Intent intent = new Intent("Msg");
Log.d("Notification", "Received");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
#Override
public void onCreate() {
super.onCreate();
Log.d("Notification", "created");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("Notification", "destroyed");
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
(Sorry for the incorrect indentation)
This is my Fragment which should respond to the broadcasts:
public class VerificationFragment extends BaseFragment implements SMSReceivedCallbacks {
public void onNotificationReceived(int code) {
ed.setText("OK");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new Receiver(), new IntentFilter("Msg"));
}
// A bunch of other useless stuff
private class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
onNotificationReceived(0);
}
}
}
The Manifest:
<service
android:name=".NotificationListener"
android:label="#string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
To summarise, I've:
02-28 11:40:53.236 6585-6585/com.******.laurentmeyer.****** D/Notification﹕ Created
02-28 11:40:53.236 6585-6585/com.******.laurentmeyer.****** D/Notification﹕ created
and then nothing. I already looked at all the threads on SO but maybe missing something really simple.
Enable Notification Access in your settings:
if (Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName())) {
//Add the code to launch the NotificationService Listener here.
} else {
//Launch notification access in the settings...
getApplicationContext().startActivity(new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}

Do not understand different NotificationListenerService implementations

I'm trying to capture notifications posted in my Android 4.3
I've followed this example: http://www.kpbird.com/2013/07/android-notificationlistenerservice.html and it works seamlessly.
What I don't understand if I replace the NLService with my implementation which stands below, the service is never created and NLService class from example is created whenever I grant permissions from Security in Settings but my class does not.
public class ListenerService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "******** Service Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
with Manifest.xml
<service
android:name=".ListenerService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
Trying to know what's happening, I left NLService like my class, so it looks something like this:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "******** Service Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
with Manifest.xml
<service
android:name=".NLService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
And again, NLService works but ListenerService doesn't.
In my final attempt, I renamed ListenerService to NLListenerService, and then, it suddenly worked, printing "Service Created" when enabling the checkbox.
Can someone explain me what's happening here? :)

Android - ACTION_PACKAGE_FIRST_LAUNCH filter not working

I trying to detect my application run at first time. I used Broadcast Receiver to do this. It works fine with ACTION_PACKAGE_REPLACED. But it doesn't work when I use ACTION_PACKAGE_FIRST_LAUNCH intent. I'm using Android 4.3
This is my Activity
public class MainActivity extends Activity {
BroadcastReceiver broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new TestBroadcast();
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(broadcastReceiver, intentFilter);
}
}
This is my AndroidManifest.xml
<receiver android:name="com.example.TestBroadcast" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
And TestBroadcast class
public class TestBroadcast extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
Toast.makeText(context, "Application installed",
Toast.LENGTH_SHORT).show();
}
}
}
Applications would receive these intents. These intents are broadcasted only to Play Store.

AudioManager and system volume change receiver

I've followed instructions on this page until "Preparing your code for Android 2.2 without restricting it to Android 2.2". Even if I'm building against 2.3 version, I guess it should be working at this point but it's not. I've registered a receiver in manifest:
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
created a class for RemoteControlReceiver declaration:
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();
}
}
and finally boostrapped it in the starting activity.
private AudioManager _audioManager;
private ComponentName _componentName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
_componentName = new ComponentName(getPackageName(), RemoteControlReceiver.class.getName());
}
#Override
protected void onResume() {
super.onResume();
_audioManager.registerMediaButtonEventReceiver(_componentName);
_audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
Toast.makeText(getApplicationContext(), "onFocusChanged", Toast.LENGTH_SHORT).show();
}
}, AudioManager.STREAM_MUSIC, 0);
}
Could you point out what am I missing here ? I have an assumption that in order to receive thodse messages I my activity has to play any media stuff. I gonna test it right away.
P.S. As you see I've even added some uneccessary code - don't pay attention to requestAudioFocus.
Thanks for any suggestions.

Categories

Resources