my application defines events that other apps installed on the device could be registered to (like other apps can register to google play services location updates and activity recognition events).
google play services process notify this types of events via PendingIntent provided by the receiving app.
I prefer to do it by sending a broadcast restricted by permissions, and I think that from a good reason: PendingIntent cannot be saved in persistent way, so every time my app will shout down, the apps that that registered to my events will have to re-register to my events..
the problem: in the link I provided, you can see how to restrict broadcast by custom permission declared in both sides (the broadcast sender application, and the receiving application), but what if I need to decide from the senders side dynamically from a list I'm getting from the server, what are the apps that allowed to receive the broadcast?
I don't find any reference or example in the web how to do so.
I guess it suppose to be possible somehow, because seems like google play services is using this approach for notifying GCM push notifications only to the relevant receiver...
please help me understand how can I do such a thing.
but what if I need to decide from the senders side dynamically from a list I'm getting from the server, what are the apps that allowed to receive the broadcast? I don't find any reference or example in the web how to do so.
Use an explicit Intent, where you have set the ComponentName of the specific BroadcastReceiver that you are "broadcasting" to. You will need to "broadcast" such an explicit Intent once per app that is "allowed to receive the broadcast".
Related
Hi I'm want to collect user health data. In iOS we have HKObserverQuery to observe.So when ever there is a change in health data for example, change in step count it will wake our app. Is there any thing similar in android for live updates. So I can setup monitoring for any changes.
I been scratching my head for few days. Thank you.
Broadcasts
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
You Could Use LocalBroadcastManager for your App when data changed it will called from your code
Then Register Reciever in manifest
send Broadcast locally fron your app to Broadcast Reciever when data changed in store Like HKObserverQuery
https://developer.android.com/guide/components/broadcasts.html
Yes, there is a CompletableFuture class in java to do so. You can read about it in Java's documentation
If there is an application receives a notification while it is not running in background/foreground. Will just receiving a notification trigger MyApplication.onCreate()? What other component will the android framework start by just receiving a notification? Thanks.
If you are talking about push notifications (which are supplied by GCM), Android will start your GCMIntentService, which you need for a working implementation (you can read more about it in the GCM integration documentation.
Before the GCMIntentService is called with the Intent, the Android OS receives a TCP packet from the Cloud Messaging servers, which contain all the data of the notification. Only 1 TCP connection is kept alive for all the notifications, to save power. Using the API keys, the OS will search for the application, which should receive the push. If it has been found, it will call the GCMIntentService of that application with the Intent containing the notification. From there on, it's up to the developer what he would like to do with that.
(To save even more power, the GCMIntentService is not running for all applications. Instead, a broadcast receiver has to be also defined, which wakes the service up when a notification arrives).
If you are talking about the Android notifications, which appear in the status bar, the application does not receive those. Those are posted via the NotificationManager system service by the apps themselves. The developers of the apps can create, update and cancel those notifications, based upon the app usage, and the events which happen.
When these notifications are created, you can add PendingIntents to them, which define the action which should happen when the user clicks on the notification. Mostly it is an intent to open a specific Activity of the application. When the specific Activity has been opened, it can check its getIntent(), which contains the intent of the notification, and any extra data which has been added to it. (Intents can contain extras, like Strings, Integers, Parcelables, etc.).
A common use case in Android apps is receiving a GCM message, and then posting a notification in the status bar. Like when you get an email in GMail. This way the user is not interrupted in his current work, but can still open the app if he wants to.
Google says: "GCM delivers messages as a broadcast. The receivers must be registered in the manifest in order to wake up the application." but What component of Android does actually get the message first? Because İ dont think the Client app is the first component that gets the message by Broadcast receiver.
Would be happy if someone can explain it A bit more or give me A link to a source. Thx
The client app is really the first component to get this broadcast by a broadcast receiver. Not only that, it is the only component to get it, simply because nobody else can listen for that specific broadcast (the one that is defined in your application's manifest).
But I guess you wanted to know who gets the message from the network and sends this broadcast. That is Google Play Services.
The only documentation on sendBroadcastAsUser says this:
sendBroadcastAsUser(Intent intent, UserHandle user, String
receiverPermission)
Version of sendBroadcast(Intent, String) that allows you to specify
the user the broadcast will be sent to.
But I've not been able to find any examples of how the user is specified, or how you would set up a listener to listen to such a broadcast.
Within the AOSP source I found this example:
Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
intent.putExtra(PhoneConstants.STATE_KEY,
DefaultPhoneNotifier.convertCallState(state).toString());
if (!TextUtils.isEmpty(incomingNumber)) {
intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
}
intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
android.Manifest.permission.READ_PHONE_STATE);
What does UserHandle.ALL mean as the specified user, if it literally means "all" then what is the point in using sendBroadcastAsUser() with ALL rather than just use the vanilla sendBroadcast()?
Would my app be able to listen to this particular system broadcast?
sendBroadcast() - broadcasts the intent only to apps running in the current user
sendBroadcastAsUser allows to send broadcast to apps running in other apps. But this requires INTERACT_ACROSS_USERS_FULL permission which is not available to 3rd party apps. This is the reason, the api is not documented
what is the point in using sendBroadcastAsUser() with ALL rather than
just use the vanilla sendBroadcast()?
sendBroadcastAsUser allows to send broadcast to a particular user , or in case UserHandle.ALL is used, to all the users.
Would my app be able to listen to this particular system broadcast?
Yes, any broadcast sent to the user in which your app is running, will be able to listen to the broadcast as long as your app has permission to recieve that broadcast
I have a requirement that my app, can receive events (e.g. messages) from server any time (even if app is not running). So do I need to create an infinitely running service to listen for these events?
This to me seems similar to Email apps in Smartphones, like whenever you receive a new email(event in my case), its able to show notifications and also able to update my list adapter whenever I receive an event.
But I dun know how will I implement this?
You should take a look at C2DM (push-messages):
http://code.google.com/intl/sv-SE/android/c2dm/index.html#intro
it allows a server to send messages to devices at any time.
From the link:
Here are the primary characteristics of Android Cloud to Device
Messaging (C2DM):
It allows third-party application servers to send lightweight messages to their Android applications. The messaging service is not
designed for sending a lot of user content via the messages. Rather,
it should be used to tell the application that there is new data on
the server, so that the application can fetch it.
[...]
An application on an Android device doesn’t need to be running to
receive messages. The system will wake up the application via Intent
broadcast when the the message arrives, as long as the application is
set up with the proper broadcast receiver and permissions.
[...]
Events from server are called "push notifications" and are implemented via "Cloud 2 device messaging" (C2DM). On the mobile side these messages are submitted as broadcast events (see BroadcastReceiver). For a complete example see some tutorials: Google, Vogella or here on Stackoverflow
I think you have to start a Service as soon as the device booted. There is a good tutorial here how to achieve this.