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
Related
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)
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 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
I've a problem which seems strange to me. I've different notifications that are thrown when an alarm goes off. Each notification has an pendingIntent that contains a serializable object. As I am working with fragments, every pendingIntent is sent to the same activity, namely the main activity. Depending on the contained object in the pendingIntent, another fragment is shown. This works perfect. The problem is if I am closing the app and open it again via the launcher icon, not the defined main fragment but the previous fragment (that has been created from the notification) is shown. As I've observed it in Logcat, it seems to be that the MainActivity that is started when i click on the launcher icon reads still the pendingIntent. Isn't the pendingIntent deleted after having been used? I've created the pendingIntent with the FLAG_ONE_SHOT....
I would be grateful for help!
Ok,
I am working on an Android app that uses a service to collect & process GPS data. The service also creates a notification in the status bar. What I cannot figure out is how to have the notification open the main class when a user clicks on it. From what I've found online it shouldn't be that hard to do, but I cannot get it to work. Does anyone have some suggestions on where to look?
Intent notificationIntent = new Intent(this, MyClass.class);
This line of code is from http://developer.android.com/guide/topics/ui/notifiers/notifications.html.
Simply replace MyClass with your activity class - it will be launched when you click the notification.