Parse.com Push notifications and ordered broadcast - android

I'm implementing Parse push notification. I have 2 receivers. First one will always show notification and second one will take over notification if one particular activity is running. One registered in manifest (priority 1) and the other is registered/unregistered dynamically (priority 2). My problem is that I cant cancel the broadcast from my dynamic receiver by calling
abortBroadcast()
it throws an exception
BroadcastReceiver trying to return result during a non-ordered broadcast
So, is there a way to make the broadcast ordered or something like that?
I really want to handle the data in my activity when it is active.

Ok, after 2 hrs of searching, finally solved it. This is what I did,
First I created a base BroadcastReceiver which receives the push notification from Parse. It then removes all actions from received intent and adds a custom action eg, com.myclass.PUSH
Then I used
context.sendOrderedBroadcast(intent, null);
to send a new ordered broadcast using my custom action.
Now I set other two receiver's (the one defined in manifest and my dynamic receiver defined in activity) action to com.myclass.PUSH
Now the broadcast is ordered and i can cancel it using abortBroadcast()

Related

Should BroadcastReceiver be declared inside activities?

I'm wondering what's the better cleaner design approach to handle BroadcastReceiver creation between those two:
Declaring directly a BroadcastReceiver in an Activity/Fragment and overriding onReceived method there.
Creating another custom receiver class in a package "receiver", that extends the BroadcastReceiver class and override onReceive. Then, you instantiate that custom receiver class in your Activity/Fragment.
There are 2 types of Broadcast Receivers
1)Dynamic Broadcast Receiver that is registered inside activity
2)Static Broadcast Receiver that is registered or declared inside Manifest file with Receiver tag with specific action(when to fire the Receiver)
Uses:Eg say receive message
1) Dynamic Broadcast Receiver is used to receive the Broadcast intent mainly to refresh the message sent through send Broadcast method.
2)Static Broadcast Receiver is used to receive the message.Here the Registered Receiver with SMS receive action will receive the message and later it sent to activity
At last It depends on developer which one to use,when to use based on requirement
There is no cleaner approach, it depends on how you Code.
Following the DRY principle, I only extend a BroadcastReceiver as soon as I notice I'll need them in different Activites.

BroadcastReceiver notification and activity messages

I'm very new to Android and I need a little assistance here.
I have an activity and a broadcast receiver.
Broadcast receiver should listen SMS Intents and:
if Activity is visible and interacting - SMS should appear in activity
if no - broadcast receiver should create notification with SMS, so if user click on that notification, the activity will be shown
So, i read a lot of articles about broadcasts and i can implement one of these variant(1 - as a inner broadcastreceiver class and 2 - as global broadcast receiver declared in manifest) , but how i can have both? Could you please propose some idea?
Ok, so in that case i post my own solution.
I declare two broadcast receiver: one, declared in Manifest, and doing post to tray notification, and second - declared as an inner class of main activity and doing post to activity components. Also i have subclass of Application class to track main activity state (i.e. visible or not). And so global broadcast receiver post any notifications only if it see that activity is hide right now.
I think its reasonably solution.

Parse Push - How to Automatically Open an activity without user action on receiving a push on Android

I have a requirement (android) where my app should run its main activity automatically when a push notification is received without the user clicking on notification in system tray. I am having a map where it shows current location, but in the push, i will receive a location, and i need my map in the main activity to move the camera to the currently received location on receiving the push, and alert the user with a custom sound. All this should happen without the user clicking on anything. Pls help. Thanks in advance.
Yes, it's possible. Parse.com documentation says:
You can also specify an Intent to be fired in the background when the push notification is
received. This will allow your app to perform custom handling for the notification, and
can be used whether or not you have chosen to display a system tray message. To implement
custom notification handling, set the Action entry in your push notification data
dictionary to the Intent action which you want to fire. Android guidelines suggest that
you prefix the action with your package name to avoid namespace collisions with other
running apps.
So you send push notifications in this way:
JSONObject data = new JSONObject("{\"action\": \"com.example.UPDATE_STATUS\""}));
ParsePush push = new ParsePush();
push.setData(data);
push.sendPushInBackground();
Then in your AndroidManifest.xml register a broadcast receiver that will be called whenever a push notification is received with an action parameter of com.example.UPDATE_STATUS:
<receiver android:name="com.example.MyBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.UPDATE_STATUS" />
</intent-filter>
</receiver>
In your broadcast receiver you can start a new activity:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, MyActivity.class));
}
}
warning to #makovkastar, major changes since version V1.8. for instance: no more com.example.UPDATE_STATUS.
push notifications guide is more clear now: https://www.parse.com/tutorials/android-push-notifications
this is the ParsePushBroadcastReceiver subclass: https://gist.github.com/panchicore/97d5ad25842258576109
this answer have a good tutorial to send/receive local broadcasts: https://stackoverflow.com/a/8875292/155293
Basically onPushReceive will be called when a push is received in the device, in this method use LocalBroadcastManager to make something in the app, for instance, add a new message to a chatroom.

start a another version of an existing broadcastreceiver

I have a fragment with a button that starts a BroadcastReceiver. This BroadcastReciever creates a notification after a time retrieved from the fragment. This part works.
However, when I press the button more than once, the existing broadcast receiver is overwritten. I don't want this as I want multiple notifications to be created.
Any solutions?
Thank you
I found the answer - The pending intent used in the alarm manager was not unique so it was overwritten each time I created another alarm. What I needed to do was create multiple alarms. This is done by adding a unique id to the 2nd parameter (argument?) of the pending intent.
See this answer for more details: https://stackoverflow.com/a/10090378/2442638

How BroadcastReceiver is different from Intent

Hello I see contradicting definitions. Android experts, can you explain this to me please?
1) If BroadCastReceiver is a component in android that responds to intents, then I can as well register an filter for activity in androidManifest xml file and have it do my job based on intent like battery low, no network, orientation change etc. these are intents I might be interested to react in my code.
2) Why register whole another filter for BroadcastReceiver in androoidManifest.xml and perform action at onReceive() inside the BCRCVR class?
3.Can we really perform intent driven operations in an activity? yes right?
I guess the title should be "How BroadcastReceiver is different from Activity".
In my opinion, Broadcast receiver is independent unit, because sometimes you don't want the system to create a new Activity object just to handle arrived intent. Moreover, you don't your activity to be shown. Broadcast receivers are independent and can be used outside any Activity. Activity is more about user interface and broadcast is about handling events.

Categories

Resources