I have 2 apps on one device. The server sends push to both applications. I want the first application that received the push to show a notification, and the second should do nothing. How can I check that the first app already showed a notification?
Apps can be closed. I use FirebaseMessagingService for wake up.
I need this because the user can have only one app installed or both.
Best way is just your server send notification to only one app. But if it is not possible, you can use Broadcast receiver I think.
You can use Broadcast receiver to communicate between two application.
In app B, for example declare broadcast receiver like follow:
<receiver android:name=".MyBCReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="what.ever.you.want" />
</intent-filter>
</receiver>
And from app A.
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("what.ever.you.want");
intent.putExtra("key","data");
intent.setComponent(new ComponentName("com.app.b","com.app.b.MainActivity"));
sendBroadcast(intent);
Then your app B, you can just make a mark using sharedPreference to not make a notification again.
Related
How chat apps (e. g. Messenger) listen to incoming messages even if their activity haven't been started yet since in android 3.1 and later this is not possible:
Manifest:
<service android:name=".ManagerService" android:enabled="true" />
<receiver
android:name=".BootFinishedReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Receiver:
public class BootFinishedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, ManagerService.class);
context.startService(serviceIntent);
}
}
There have to be some way around as chat apps are still working this way.
Thanks for any informations or ideas
Your question is quite open-ended and broad. But to the link that you have pointed about the broadcast receiver when the app is not running.
There is a comment on the same answer that says:
Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications). That means, the user should launch the app at least once after installation to activate the application, then the app can receive all implicit broadcast from OS as normal.
The app is stopped when it is just installed. As soon as you launch the app for the first time, The application can listen to broadcast receivers and can run background services even when the app is closed.
The chat applications basically implement socket.io that keeps up the communication on both ends. Furthermore, you may implement FCM to get notifications and messages even when the app is killed.
I hope you, understand the concept :).
In my app whenever we get the message, need to display one pop up with pre filled message. For this i used the following code in manifest file
<receiver
android:name="com.cte.broadcast.SMS_Receive_BroadCast"
android:enabled="false">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
And whenever app is login, register the broadcast receiver by using the following code
ComponentName component = new ComponentName(getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
And whenever app is login unregister the broadcast receiver by using the following code
ComponentName component_sms = new ComponentName(context.getApplicationContext(),
SMS_Receive_BroadCast.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component_sms,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
so here everything is working fine. when ever we logged in and whenever we get the message broadcast receiver trigger and getting pop up even app is closed. and logged out not getting trigger broadcast receiver and not getting pop up. so up to this everything fine. But the problem is after one day whenever we get the message the broadcast receiver wont trigger even it is logged in. But whenever we open the app and close it then it will working fine. so the problem is after some time broadcast receiver is automatically unregisterd i think..
So how to resolve this problem..Thanks In Advance..
Some applications abort your Broadcast on the intent which will prevent other applications from receiving the intent.
The solution is to increase the android:priority attribute in the intent-filter tag:
<intent-filter android:priority="priority value">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
I had a similar trouble. I dont know exactly why, but depending on you put your registerBroadcast, android will kill that instance, it´s something related to processes execution stack on linux.
What i did to solve the problem: create a background service instead of a BroadcastReceiver, there you register your BR and it will work fine, several days =))) !
In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.
I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:
Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"
Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);
I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:
<receiver
android:name=".receivers.ReportingReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.app.FILTER_INIT_REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Curious why the ReportingReceiver class is not getting the intent call?
If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.
Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.
See http://developer.android.com/about/versions/android-3.1.html#launchcontrols
As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.
Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.
http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES
You can read more about this Android 3.1 change here
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
and here
http://code.google.com/p/android/issues/detail?id=18225
i am trying to pass data (string\int) between two service in two different application
service A in app1 and service B in app2.
say i want to pass from application 1 -> 2 , so i defined a custom receiver in the manifest in app 2 with an intent filter with an action string
<receiver android:name=".blaReceiver">
<intent-filter>
<action android:name="com.bla.blabla.RANDOM_ACTION" />
</intent-filter>
</receiver>
but how do i send the intent from app 1 ?
there are no activities only services, i thought of startService from app1 but there is no where i can define it to sent the intent to app2
Thanks.
Use broadcast receivers. Send broadcast [with unique action name]from app A and register a broadcast receiver in app B. [broadcast will be sent system-wide]. Receive broadcast in you app B and verify it by action name.
You can use sendBroadcast(intent); in your service. It works for sure.