Call a method from my "MainActivity" in Android - android

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).

Related

Don't receive FCM notifications when a fragment is open

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.

Broadcast receiver in activity Android

Is it possible that If I place some part of the code from my Activity in Broadcast and then even if the activity is not running the broadcasted piece of code will continue to work?If yes any reference to it plz?
To b specific I've facebook activity that retrieves notifications on button click.I want that get notification method to b in a broadcast so that every time notification arrives it generates alert.Can provide the code aswel.. Kindly help
Is it possible that If I place some part of the code from my Activity
in Broadcast and then even if the activity is not running the
broadcasted piece of code will continue to work?
No, it is not possible that your activity is not running and you defined your broadcast in the activity and you still want the broadcast to run that is within that activity.
Add the BroadCast Receiver in a Service class. register and unregister this broadcast receiver from onCreate() and onDestroy() methods respectively of Service class.

Reload an Activity when a notification arrives, Android

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.

Check if there is a phone call going on from broadcast receiver

I have an application that is launched from a broadcast receiver, but I don't want to launch the application if the user is currently in the middle of a phone call. How would I check from my broadcast receiver class if there is currently a phone call going on?
You should create a service with a PhoneStateListener. Make sure it gets unregistered once you get what you need from it and make sure not to launch the listener directly from your broadcast receiver. There are some more details concerning this concept in this question.

Receivng intent broadcasts when application is shutdown

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.

Categories

Resources