Reload an Activity when a notification arrives, Android - 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.

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.

How to start a broadcast receiver on a separate global process from that of the application?

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.

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.

Show I open my activity inside the onReceive of BroadcastReceiver when received a Push Notification

When using Android Push notification, the onReceive of the registered BroadcastReceiver is being called when the device receive the notification.
The issue is should I open my activity immediately inside the method onReceive? Because sometimes the app might be closed, so forcing open the activity when receiving the notification seems rude, right?
Are there any best practice for this?
`
Well it is rude to open an activity as soon as you recieve a push, since user might be in the middle of doing something important.
The best practice is to post a Status notification which on clicking should take it to the activity which is relevant to the push message.

Call a method from my "MainActivity" in 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).

Categories

Resources