Are GCM messages sent as an ordered broadcast? - android

I have seen a few posts that mention that GCM messages are sent as ordered broadcasts, and that the "android:priority" attribute hence can be used to to control which (out of several) receiver(s) that should handle the broadcast first.
For example: GCM BroadcastReceiver setResultCode use
However, I can't find any doc from Google that confirms this. Regardless of which priority I set on my different GCM-receivers, the broadcast seem to be un-ordered.
Are GCM messages really sent as ordered broadcasts?

I think I've found out why I couldn't control my broadcasts (which led me to believe that GCM broadcasts aren't ordered): I forgot to call abortBroadcast() to prevent the second broadcast receiver from handling the broadcast message:
http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast() :
Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast.
So when I call abortBroadcast() only the first receiver handles the broadcast.

From Google Documentation
A "message with payload" is not simply a "ping" to the mobile application to contact the server to fetch data. In the aforementioned IM application, for example, you would want to deliver every message, because every message has different content. To specify a non-collapsible message, you simply omit the collapse_key parameter. Thus GCM will send each message individually. Note that the order of delivery is not guaranteed.

Related

Android Broadcast own message

Is it possible to broadcast any message from any Service which is running in background so that any broadcast receiver from that app or any other app can receive the message?
The broadcastreceivers must have some filter and if your broadcast doesn't set this filter your broadcast wouldn't receive the broadcast.
So if you would like to send some message to all of your broadcastreceivers, then all of them should have the same filter properties, but then they are unnecessary.
See the documentation for further details.
When you use sendBroadcast(Intent) or related methods, normally any other application can receive these broadcasts. You can control who can receive such broadcasts through permissions described below.
-> http://developer.android.com/reference/android/content/BroadcastReceiver.html
I don't think so. The service should send a specific message, and only the apps that listen for that message will intercept it.

android: custom intent is triggered first

I am writing an app to receive the sms and display it on my screen. I declared a broadcast receiver and registered in my activity (in oncreate()). I haven't set any priority in my manifest file as well.
However during run time, my receiver is receiving the sms first then the systems message service is invoked first. Is this the behavior of custom broadcast receivers?
Thx!
Rahul.
Android sends ordered broadcasts for SMS messages. From the reference guide (emphasis mine)
Ordered broadcasts (sent with Context.sendOrderedBroadcast) are
delivered to one receiver at a time. As each receiver executes in
turn, it can propagate a result to the next receiver, or it can
completely abort the broadcast so that it won't be passed to other
receivers. The order receivers run in can be controlled with the
android:priority attribute of the matching intent-filter; receivers
with the same priority will be run in an arbitrary order.
I suspect that due to the lack of priority, the order is being chosen arbitrarily.

How to prevent incoming messages by reaching the native inbox in android?

I am developing the app which holds the messages in it's own inbox rather than the native inbox.
This can be achieved only if the incoming messages are prevented from reaching the native inbox.
There is some attribute android:priority in <intent-filter>, by which message can be first received by current receiver than others.
But I do not know how to do it properly, does anyone know?
I assume you mean SMS messages? The best, and only AFAIK, is to receive the message via registering the proper BroadcastReceiver object, copying the message into your database, marking it is read in the SMS ContentProvider and then deleting it from the SMS ContentProvider. Check out this article on how to receive SMS messages and then check out this SO post on how to use the SMS content provider.
Edit I actually stand corrected, there is another method that I've had sometimes limited success with. In the BroadcastReceiver you can call the abortBroadcast() method to cancel the broadcast. The important thing here is that you register your receiver with an intent filter that has a higher priority (say, 100) so that it receives the SMS before anybody else does.

Are broadcast receivers guaranteed to receive every broadcast announcement intended for them?

A fellow developer and I have started to question whether a broadcast announcement is guaranteed to be received by the appropriate broadcast receiver.
We have a broadcast receiver which receives messages that should be put on to the screen. Occasionally we notice that some messages never make it to the screen.
Even with the debugger, it has been hard to tell for certain if the receiver is not getting the broadcast or if it due to our own bugs that the broadcast is never sent.
So I wanted to ask generally if there are any known reasons why the broadcast receiver would not receive an announcement?
There are ordered broadcasts in Android. They are sent to receivers according to the receivers priority. And receiver with a higher priority can abort the broadcast spreading.
See this blog post for details.

Android: SMS filter - Can you have priority listener for SMS messages?

I am trying to implement an app that will only listen for a certain header in the SMS.
(Parse the sms message and look for some header)
That's not hard to do.
The problem is once the app finds the header and knows this SMS is for me,
I don't want other app(that has broadcast listener on SMS) to receive SMS.
(for example, 'messages' to pop-up with sms message)
Is there a way to listen SMS before any other apps and decide to pass on to other app
or don't pass to other apps?
See one of the answers for the following question Can we delete an SMS in Android before it reaches the inbox?. It explains how to receive the BROADCAST, set the priority of your app so you receive the broadcast first so you could do your checks then cancel the broadcast so no other app receives it.
Read about BroadcastReceiver. You should use ordered broadcast - use android:priority attribute, and then call abortBroadcast() in your BroadcastReceiver.

Categories

Resources