I am trying to build a chat application and I successfully implemented fcm notifications whenever a new message comes in. But my problem is when I am chatting i don't want to receive fcm notifications, since i am in the chat window (My chat fragment) anyway.How can this be implemented?
Here is an interesting approach with Ordered Broadcasts by Commonsware.
Define an action string you will use when the event occurs that you
want to go to the activity or notification (e.g.,
com.commonsware.java.packages.are.fun.EVENT).
Dynamically register a BroadcastReceiever in your activity, with an
IntentFilter set up for the aforementioned action string and with a
positive priority (the default priority for a filter is 0). This
receiver should then have the activity do whatever it needs to do to
update the UI based on this event. The receiver should also call
abortBroadcast() to prevent others from getting it. Be sure to
register the receiver in onStart() or onResume() and unregister the
receiver in the corresponding onStop or onPause() method.
Register in your manifest a BroadcastReceiver, with an
<intent-filter> set up for the aforementioned action string. This
receiver should raise the Notification.
In your service (e.g., an IntentService), when the event occurs,
call sendOrderedBroadcast().
Full description here.
Related
I have an android application where I am implementing push notifications using GCM. I have a broadcast receiver and an intent service for handling GCM Push notifications. My requirement is that I want to receive the notification inside OnReceive of the broadcast receiver in such a way that the Application doesn't get created before that.
According to Android Developer docs :
Called when the application is starting, before any activity,
service, or receiver objects have been created. Note that content
providers are created before the application object.
I have tried to start the broadcast receiver in a separate Global Process from that of the application, but on doing this the GCMBroadcastReceiver doesn't get triggered at all on sending a notification.
Is this possible at all, if yes how?
I want to receive the notification inside OnReceive of the broadcast receiver in such a way that the Application doesn't get created before that
That is not possible, sorry.
I am a bit confused about this part of documentations:
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent).
Once your code returns from this function, the system considers the object to be finished and no longer active.
What does "no longer active" mean? It means our receiver won't receive any events with the specified type? Or it means the receiver is destroyed and a new one is created when the event occurs?
Is this any difference about this, between registering receiver in manifest file and in code dynamically?
"Is this any difference about this, between registering receiver in manifest file and in code dynamically?"
Yes, there is a difference. Receivers registered in the manifest file will receive all intent-filters it matches, anytime the OS sends them out. This differs from registering a broadcast receiver in code by the broadcast receiver in code will only listen to broadcast intents while you have set it to listen. The reason you would use this method sometimes, as opposed to just registering it in the manifest, is if you wanted to implement ordered broadcast. For example, (you can build your application in such a way that) a broadcast receiver in an activity would have higher priority than the manifest, that way, if you receive an intent that your application handles, you can present a message in the activity because you know that the user much currently be in your app. If the broadcast receiver is not listening in the activity, then you assume the user is not currently using your app so then you may just want to send a notification. I should mention that ordered broadcast have the ability to abort the propagation of a broadcast intent to the next receiver, which is what you would do if you caught the intent in your activity class ,therefore, the manifest file will only get the intent if the receiver in the activity class did not catch it.
The words "no longer active" mean that the broadcast receiver will just stop doing any work for that particular broadcast. It will still listen to any succeeding broadcast intents just fine.
It means that upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.
You will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.
I am developing an Android app that need to change the interface when a push notification arrives,
Example:
User start the Activity A, then He goes to Activity B.
While User is reading a text in Activity B,a Push notification arrives, the text in Activity B should change and the text in Activity A should change too.
I can do it on Activity A with onResume() but I dont know how to do it in Activity B
The GCM sample that comes with the GCM library (in your Android SDK Folder/extras) gives you an example on how to do this:
in GCMIntentService onReceive method, they called displayMessage, which broadcasts a message after a GCM notification is received.
in DemoActivity::onCreate, they register a receiver to handle the broadcast message.
the onReceive method of the broadcast receiver mHandleMessageReceiver performs the UI update.
If you want to receive updates when notification arrives then you need to implement Broadcast Receiver. You need to register the receiver in onResume and unregister it in onPause.
The place you need to implement to update your text views is the onReceive method of Broadcast listener. You can easily find some examples by searching "Android Broadcast Receiver". Hope it helps.
I'm using C2DM the first time and I'm looking for a general advice how I can achieve the following:
Upon receiving a C2DM messages I decide:
- if the application is upon the current activity will display an "alert popup".
- if the application is not open I'd like to send a message to the notification bar (similar to new emails, sms, twitter etc.)
We have a GlobalBroadcastReceiver extends BroadcastReceiver which implements public void onReceive(Context context, Intent intent). This is the only receiver registered in AndroidManifest.xml.
So basically all our broadcasts are piped through this receiver and the first scenario is no problem.
However I'm, wondering how to tackle the second problem. How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed and then: how can I notify the user about the incoming data?
I'm super confident there are already a lot of solutions out there but since I couldn't find them I think I'm just missing something of the bigger picture.
How can I make sure I receive a C2DM.RECEIVE broadcast even when my application is closed
Have your receiver registered in the manifest, per the C2DM documentation.
then: how can I notify the user about the incoming data?
Raise a Notification.
Since your receiver will not necessarily know if there is an activity of yours in the foreground, the best solution is to send your own broadcast Intent, but one that is ordered. Have the activity register a high-priority BroadcastReceiver for your own broadcast, and have another manifest-registered BroadcastReceiver implement a normal-priority BroadcastReceiver for your own broadcast. If the activity gets the broadcast, it displays your popup (ick) and aborts the broadcast. If your "backstop" BroadcastReceiver gets the broadcast, it displays a Notification. Here is a blog post with a bit more detail on this pattern, and here is a sample project demonstrating this use of ordered broadcasts.
I'm currently messing up with the Google's C2DM notification service.
Following the steps in this tutorial: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html, I succesfully recieved a "push message" from the server.
However, in the "protected void onMessage" I need to send the message to the "MainClass" to print it in a toast. Since I'm not deeply familiarized with the Android developing, I will appreciate any help on this. Thank you
Use a broadcast to communicate with the activity.
In onMessage send a broadcast.
In your activity onResume register a broadcast receiver and make it display a toast (remember to unregister it in the onPause)
You would need also to handle the case when the activity is not running (maybe display a notification). In this case, make the broadcast an ordered broadcast. The broadcast receiver in the activity should be set with a high prio, then register a default broadcast receiver through your manifest (this one displays a notification, or opens the activity, or whatever you want).