How to send BroadCast in Oreo? - android

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);
}

Related

How to detect a notification in 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

onReceive doesn't get invoked for BroadcastReceiver

I tried to register the broadcast receiver in the manifest but the onReceive method isn't invoked. What am I doing wrong?
manifest.xml
<receiver
android:name="zzz.yyy.xxx.ui.storyboard.discussion.DiscussionFragment$MessageCountReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT" />
</intent-filter>
</receiver>
Sending broadcast as below:
Intent intent = new Intent("zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT");
intent.putExtra(BundleKeys.notification_post_id_forum,postId);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Receiving broadcast in Fragment class as below:
#Override
public void onPause() {
clearUnreadCount();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(messageRefresher);
super.onPause();
}
public class MessageCountReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String noti_post_id = intent.getStringExtra(BundleKeys.notification_post_id_forum);
int tempCount=PreferencesIMDStar.getIntValue(getActivity(),noti_post_id);
tempCount++;
PreferencesIMDStar.setIntValue(getActivity(),noti_post_id,tempCount);
if (getActivity() != null && noti_post_id.equalsIgnoreCase(post_id)) {
discussionPresenter.fetchCommentReply(post_id, true);
}
}
}

To catch the moment of receiving SMS

I'm creating an application that needs to read incoming SMS. Saving to a SMS provider is not required - just showing the toast at the time of receiving SMS.
<receiver android:name=".SmsHandler"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Receiver
public class SmsHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
}
}
Receiver working when app running, if app kill, receiver not call.
I know that with Android KitKat you need to specify the default applications for working with sms. But this is necessary if you want to save SMS to the SMS provider. This is not required if you need to catch the moment of receiving SMS.
What should I do to make the receiver work when the application is killed?
UPDATE
I tried to use the service to register BroadcastReceiver
public class AlarmService extends Service {
private static BroadcastReceiver SmsHandler;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate()
{
registerScreenOffReceiver();
}
#Override
public void onDestroy()
{
unregisterReceiver(SmsHandler);
SmsHandler = null;
}
private void registerScreenOffReceiver()
{
SmsHandler = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receive sms", Toast.LENGTH_LONG).show();
}
};
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(SmsHandler, filter);
}
}

BroadcastReceiver within Service Can't Receive

I register a BroadcastReceiver within MyService,
public class MyService extends Service {
final static String ACTION_ONE = "one";
final static String ACTION_TWO = "two";
BroadcastReceiver receiver;
and a inner class
// use this as an inner class like here or as a top-level class
public class MyReceiver2 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do something
Log.d("tag", "received! MyReceiver2");
}
// constructor
public MyReceiver2(){
}
}
onCreate method:
#Override
public void onCreate() {
receiver = new MyReceiver2();
IntentFilter filter = new IntentFilter();
filter.addAction(MyService.ACTION_ONE);
filter.addAction(MyService.ACTION_TWO);
MyService.this.registerReceiver(receiver, filter);
}
And the code below is in MainActivity to send a broadcast:
sendBroadcast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MyService.class);
intent.setAction(MyService.ACTION_TWO);
sendBroadcast(intent);
}
});
I can't understand why MyService(started, and is not yet destoryed) can not receive broadcast.
If I change the receiver variable to a MyReceiver class that extends BroadcastReceiver and has already been proved to work,
MyService is still not receiving broadcast.
myReceiver = new MyReceiver();
Here is MyReceiver(it is proved that it works well):
public class MyReceiver extends BroadcastReceiver{
public MyReceiver(){
Log.d("tag", "MyReceiver Constructor");
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d("tag", "received");
}
}
============EDIT================
manifest
Both
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="one" />
<action android:name="two" />
</intent-filter>
</service>
and
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
do not work.
This looks like a manifest problem,
Please make sure you are declaring that receiver in the manifest!
for example:
<receiver android:name=".receivers.BackgroundTasksReceiver"/>

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"));
}

Categories

Resources