Android: when Activity receives a broadcast how to detect if sender is the Activity itself? - broadcastreceiver

My Android app uses LocalBroadcastManager and BroadcastReceiver to communicate between activities. How do you have an activity detect when it is receiving an Intent that originated from itself rather than another Activity? In other words, how do you get the broadcast caller and ignore it if it equals this.

I'm not aware of if there's a specific way of doing this, but, I would add a specific action to my Intent that is being broadcast. It could look like this:
Intent broadcastIntent = new Intent(MyClass.class.getName());
sendBroadcast(broadcastIntent);
Then when you receive the broadcast, you'll simply just check for the specific action:
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MyClass.class.getName()) {
// Ignore the broadcast as it's the caller calling itself...
} else {
// Do something with the broadcast...
}
}
There might be other ways to do this (like just putting extras into the Intent and then reading it out in the onReceive method), but I find this to be a really simple way of getting who the caller is.

Related

Flutter - myBackgroundMessageHandler never called in push notification

I am using firebase_messaging library with firebase for push notification in flutter. The documentation says that myBackgroundMessageHandler is called when the app is in background. But it has not happened.
I would like to format the title and the body of the notification before it is shown. I am getting notifications when the app is in the background but that particular method is not called. Currently it just shows the exact text sent by backend in title and body.
Is there something I have to do to enable(?) this method? I have it in my fcm.configure method:
_fcm.configure(
onBackgroundMessage: myBackgroundMessageHandler,
)
static Future<Map<String, dynamic>> myBackgroundMessageHandler(Map<dynamic, dynamic> message) async {
//some code here
return message;
}
Thanks in advance!
I had the same problem.
onBackgroundMessage apparently gets triggered only for data-messages.
See the difference here:
https://firebase.google.com/docs/cloud-messaging/concept-options
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
Data messages, which
are handled by the client app.
In my case I had to remove the notification (subject, body etc), but also styling like bigpicture completely. Try sending only a data object.

FCM: How to bring the app from background to foreground?

I have an app in which I am using FCM (Firebase Cloud Messaging).
As given in the README, I've managed to get it all working and the event handlers for onResume, onMessage, onLaunch are all firing properly.
The onBackgroundMessage which I think is supposed to run whenever the notifications appear while the app is inactive, needs a top-level dart function.
I need this function to render a particular widget, say, IncomingCall. But this being a top-level function, the best I could think of, is to invoke the main():
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
main();
}
which then calls runApp(IncomingCall()) but even this is not working.
So how does one render a widget in Dart when the app is inactive?

Send trigger to onModule that RPC succeded

I have a Class that contain all the RPC.
One of them is :
public void authenticateUserCall(String username,String password ,DBConnectionAsync rpcService)
{
AsyncCallback<UserIdent> callback = new AsyncCallback<UserIdent>() {
public void onFailure(Throwable caught) {
Window.alert("Wrong Username or Password");
}
public void onSuccess(UserIdent result) {
Window.alert(result.getUserName());
}
};
rpcService.authenticateUser(username, password, callback);
}
If the RPC succeeds, I want to change the layout of the page to the main page for the user.
So how can I send to the onModule that the RPC succeeded ?
I don't want to build the layout in the onSuccess, and I can't clear the login layout because I don't have it on the onSuccess method.
What is the right way to do it ?
Option 1:
A couple of suggestions would be to pass in the AsyncCallback as an argument to the authenticateUserCall. This way the caller could handle on success.
Option 2: Recommended
Another option, which will give you much more flexibility is to use the EventBus to fire a custom AuthenticationEvent. I use this for handling things like roles. So for instance when the user is authenticated successfully it will return me some information about the user, like their username, password and role. I then use the EventBus to fire an AuthenticationEvent which contains this information about the user. Any of my views and activities can register for this event on the event bus and handle accordingly. So for example many of my views will disable functionality if the user is not an admin. When one of my activities handles this event it will grey out action buttons that require admin access.

gwt-rpc delay window refresh

I have the following code in the onModuleLoad() of my application:
Window.addWindowClosingHandler(new ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("If you choose to close, application will sign out");
}
});
//sign out on close
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
sendLogout();
}
});
The sendLogout() function looks like this:
// Set up the callback object.
AsyncCallback<String> callback = new LogoutCallback(this);
// Make the call to the survey service.
SurveySystemService.Util.getInstance().logout(details, callback);
Where 'details' is some object.
It works just fine when the window is closed, but if I try to refresh the page, it doesn't log out. What I figured is that since the call is asynchronous, it doesn't finish getting the message off to the server before the module is restarted.
I've tried:
1. creating and calling the callback inside the onClose method.
2. using a Timer to check if the call was made.
3. Endless loos which check the same as the above (I got desperate).
In all of these solutions, the program would reach the callback creation, but the server never received anything.
any help with this?
Can you just call logout any time the page first loads. Due to the stateless nature of the web the GWT application will not know the difference between someone hitting refresh or just navigating to the page.
You can store an ID variable in session storage which should be maintained until the browser window or tab is closed. If on the application start the ID variable exists in session storage you can use it to trigger the log out.

How to know that when the user goes offline in AgsXMPP?

I am currently trying with agsXMPP library ,
i added users to a treenode on OnPresence event.
its works well, but when the other user goes offline i want to update the tree node,
for that i need his Offline Presence ,How i get result when a chat buddy goes offline ?
When a buddy goes offline you get a OnPresence event with the Type of unavailable.
private void XmppCon_OnPresence(object sender, Presence pres)
{
if (pres.Type == PresenceType.unavailable)
{
// user goes offline
}
}