Hi all,
I had a further look into Android push, and there is a thing I don't understand,
maybe there is an issue:
In Registrar.onPostExecute() there are these lines:
ComponentName component = new ComponentName(context, AGPushMessageReceiver.class);
int status = context.getPackageManager().getComponentEnabledSetting(component);
if (status == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
// register AGPushMessageReceiver
}
What should this code do? Should it check if a receiver is registered already?
If yes, the problem with this is, that the if statement is always true,
regardless of whether there is a receiver registered in the manifest or not. So
it registers a second one, if there is already one in the manifest. Tested by
setting a breakpoint to the receivers onReceive() method, it's called twice for
each notification.
An alternative way to check if a receiver is registered is this:
String action = "com.google.android.c2dm.intent.RECEIVE";
Intent intent = new Intent(action);
List<ResolveInfo> resolveInfos =
context.getPackageManager().queryBroadcastReceivers(intent,
PackageManager.GET_INTENT_FILTERS);
boolean isRegistered = false;
String receiverName = AGPushMessageReceiver.class.getName();
for(ResolveInfo resolveInfo : resolveInfos){
if(resolveInfo.activityInfo.name.equals(receiverName)){
isRegistered = true;
break;
}
}
if(!isRegistered){
// register AGPushMessageReceiver
}
I can send a PR if you want to.
Furthermore I'm not sure if it really makes sense to register a
BroadcastReceiver for push notifications programmatically. The reason is, that
this receiver is tied to the lifecycle of the app. That means after a reboot no
messages are received until the app is started, and when Android destroys the
app after some inactive time, no messages will be received anymore too. See the
note in
https://developer.android.com/reference/android/content/Context.html#regi...,
android.content.IntentFilter):
"..the lifetime of such a registered BroadcastReceiver is tied to the object
that registered it."
I confirmed this by commenting out the receiver in the PushEE demo app manifest.
What do you think?
Regards,
Marc