Reputation: 476
I have implemented Parse Push Notifications service on my Android application, and added these lines to AndroidManifest.xml
:
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.package.name" />
</intent-filter>
</receiver>
Push notifications work well, and when the users starts the app by pressing the push notification alert, the adequate activity launches. The push notifications I send are of the following form:
alert: "message to show",
type: "type_1",
data: "some_data"
Now, the thing I could not figure out how to do, is that, only when being on a concrete activity (e.g. MyActivity
), i want to "intercept" push notifications having some value for the type tag (e.g. those being "type_1") and execute a method (e.g. refreshView
) on that activity to launch an async task to retrieve data from a server and refresh some visual elements according to it. If the user is in another activity (not MyActivity
), the push notification should be received as normal, without being intercepted.
I tried to implement a broadcast receiver but did not succeed. How can I achieve this?
Thank you.
Upvotes: 1
Views: 1722
Reputation: 663
You could try to dynamically register and unregister a proper Receiver
for notifications.
So for example in the activity you want as "listening" try:
private BroadcastReceiver receiver; // Maybe is a good idea to subclass ParsePushBroadcastreceiver...
IntentFilter filter;
@Override
public void onCreate(Bundle savedInstanceState){
filter = new IntentFilter();
filter.addAction("SOME_ACTION");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Received!
}
}
registerReceiver(receiver, filter); // Actually don't know if it is necessary since i register the receiver in onResume() also...
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReciever, mIntentFilter); // Listen when activity goes in foreground
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receive); // Don't listen when activity is not in foreground
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver); // Avoid LEAKED RECEIVER!
}
As for now I did not try this solution, but if you go with this, you shouldn't use a manifest registered receiver, because it will no longer depend on application lifecycle (i.e. Receiver runs regardless of the state of your application).
If you wna t to keep the android manifest registere receiver, maybe a good idea could be to have a static boolean variable set to true when the listening activity goes in foreground and false when it goes off, and then the receiver could do action based on that boolean value.
Anyway, take a look at Context.registerreceiver() and BroadcastReceiver.
Cheers!
Upvotes: 1