I have Firebase implemented at application level to receive push messages. In the library project, I have a BroadcastReceiver to intercept push messages. I want to build notification from the library and not from the application.
Incase of app in foreground state, when I create a Pending intent, the MainActivity context available at the app level is passed in the pending intent, upon which if the notification is tapped, I am redirected to MainActivity. Now if the app is in killed state and push message is received, my broadcast receiver in the library intercepts the incoming message, but not able to create notification because the MainActivity context is null as the MainActivity at app level is not available in the stack trace. Can anyone assist how to achieve this?
Below is my code :
val mIntent = Intent(applicationContext,activityContext::class.java)
val pendingIntent = PendingIntent.getActivity(applicationContext, System.currentTimeMillis().toInt(),
mIntent,PendingIntent.FLAG_UPDATE_CURRENT)
where, activityContext in the Intent is the context of MainActivity received inside the library project from app level.
When app is in foreground state, activityContext is available and notification gets generated. But when app is in killed state, activityContext remains null leading to failure in building the notification.
You don't need the Activity's Context, because if the app isn't running, it doesn't exist.
You should have the app pass the library the name of the MainActivity (ie: fully qualified class name). The library can then use the name of the MainActivity to set the component parameters in the Intent.
This means instead of this:
val mIntent = Intent(applicationContext,activityContext::class.java)
You want to do something like this (my Kotlin syntax may be wrong, sorry):
val mIntent = Intent()
mIntent.setClassName(applicationContext, nameOfMainActivity)
Related
i am building a flutter plugin in java that creates a media notification, and i am having a problem getting the notification click to bring the app to foreground, it used to work on previous android versions but not when testing it on S (31) as a target.
if found this section in the docs :
As of Android Build.VERSION_CODES.S, apps targeting API level Build.VERSION_CODES.S or higher won't be able to start activities while processing broadcast receivers or services in response to notification clicks. To launch an activity in those cases, provide a PendingIntent for the activity itself.
I would like to know if thi is the case or not, or if i need to check something else, here is the code i am using to create the intent i set in the notification builder :
private PendingIntent createContentIntent() {
Intent openUI = new Intent(mContext, AudioplayerPlugin.class);
openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(mContext, REQUEST_CODE, openUI,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);
}
AudioplayerPlugin is the plugins own name, the app itself is another activity.
I'm using the FCMMessagingService class, and in on message received method, I'm creating a pending intent for Activity4.class and passing the extra value as below.
val openIntent = Intent(this, Activity4::class.java)
openIntent.putExtra("isPush", true)
When a push notification is tapped on, can I get the extra data when the app is in the foreground and background with the logged-in case?
But I'm not receiving this extra data when the app is not launched, or the app is killed and relaunched.
In the manifest, the launcher activity is LaunchActivity.class. Here, I can see that pendingIntent and launcher activity are different. How can I get the data in this scenario?
I have a service that is activated when alarm is activated. It extends IntentService, and in that service I need to send a message to MainActivity. The MainActivity on receiving this message then performs some necessary work. I send the message to MainActivity using :
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
This works OK if the MainActivity is currently active (displayed). If however the MainActivity is not currently active, I create it using :
PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_ONE_SHOT);
mPendingIntent.send(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
If the MainActivity is currently active (displayed), then the message reaches it. However, when I have to re-create the MainActivity using a PendingIntent, the message sent via LocalBroadcastManager does not reach the MainActivity. If I sleep for eg. 2000ms, before sending the message (where the MainActivity has to be recreated), the message does reach the MainActivity.
While this may be understandable, I would prefer to use a fail-safe way of ensuring that the app works as intended.
To guarantee that the MainActivity has received the message, do I need to send a message from the MainActivity to the service to verify that the message has been received?
Is there a better way to handle this?
Is there a better way to handle this?
Only use LocalBroadcastManager when the activity already exists. If you have to start the activity, include the information that you would have sent in the broadcast in the Intent that you use to start the activity. The activity can then look for that information in onCreate() and use it; otherwise, it can behave normally.
I am using Firebase Cloud Messaging and I have my own class that extends FirebaseMessagingService.My activities order are as follows:
SplashActivity -> MainActivity -> DetailsActivity . When the app is in the MainActivity and I send a message using the Firebase Console everything is fine it opens up the Dialog I wanted it to open. Also when I am in MainActivity and I send a message that is meant to open DetailsActivity everything is fine.
However when the app is in the background and I send the message, the Notification does not show as intended and when I click it it opens SplashActivity first even though the defined Intent to be opened using PendingIntent is MainActivity. How can I go about these two problems?
In my custom FirebaseMessagingService class I have defined the Intent to have the following flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP and my PendingIntent to have the following flags PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT but it still does not work.
Check if method onMessageReceived in your FirebaseMessagingServise is properly called in background, so you can setup what you need.
I had to send data message type instead of notification, because when you send type notification then method OnMessageReceived is not called in background and firebase handles push notification instead of you. Be aware you cannot even combine type notification and data message.
More about data message you can find here https://firebase.google.com/docs/cloud-messaging/concept-options
I am fairly new to Android, and am currently working on a simple XMPP Client. A user should be able to log in, and should be notified whenever an XMPP message arrives. It should be possible to access an overview of all messages that arrived during the current session.
When launching the application, a LoginActivity is started, prompting the user to fill in his or her credentials. If the right credentials are provided, a background service is started:
Intent intent = new Intent(this, NotificationService.class);
startService(intent);
On startup, the notification service adds a packet listener to the XMPP connection and requests to be running in the foreground. The users is prompted with a notification caused by this foreground request ("Ongoing"). Now I have provided a second activity called XMPPClientActivity, showing all messages that are received during the session and a simple logout button. When opening the application from within the "Ongoing" notification, the XMPPClientActivity is started because the notification is defined like this:
xmppIntent = new Intent(this, XMPPClientActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, xmppIntent, 0);
NotificationCompat.Builder xmppBuilder = new NotificationCompat.Builder(this);
xmppBuilder.setContentIntent(pendingIntent);
// Notification details
startForeground(id, xmppBuilder.build());
When opening the application from the home screen however, the LoginActivity is opened again. Of course I want the XMPPActivity to be started, but I can't seem to figure out how this should be done. I have been looking into binding an activity to a service, but I'm unsure if this can be of any help. What is the right way to do this?
What you can do, as I understand you issue, is use ShaeredPreferences. Create a preference like "loggedin" and set a boolean variable to true the first time they log in. Now you can set this to false when they click the "logout" Button.
When the Activity is started you can check the SharedPreference before calling setContentView() and if the value is true then finish() the LoginActivity and open your other Activity.
The link to the docs I provided has a good example of creating, opening, and editing SharedPreferences