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?
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 need to implement this feature but I am not sure if this is possible on android.
We have gcm listener service running. But let's say the app is open, and a notification arrives from the server. I need to have an activity triggered automatically without touching the notification status bar on the top of the phone screen.
In other words, without any user interaction once the notification arrives, if the app is running, an activity must be triggered immediately.
I took a look at this thread, but this is not really what I need.
Intent - if activity is running, bring it to front, else start a new one (from notification)
any clues or more info?
thx!
You can start an activity without another prior activity by using the FLAG_ACTIVITY_NEW_TASK flag.
Context c = getApplicationContext(); // or getContext(), or any other context you can find from the current app
Intent i = new Intent(c, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
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 have a service that shows a notification that I wish that will be able to go to a specific activity of my app each time the user presses on it. Usually it would be the last one that the user has shown, but not always.
If the activity was started before, it should return to it, and if not, it should open it inside of the app's task, adding it to the activities tasks.
In addition, on some cases according to the service's logic, I wish to change the notification's intent so that it will target a different activity.
How do i do that? Is it possible without creating a new notification and dismissing the previous one? Is it also possible without creating a new task or an instance of an activity?
No it wouldn't be possible to change the Activity once you have sent the notification.
You can start an Activity on your task stack that is not a problem, check out the notification service in the tutorial here:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
You have to set a pending intent on the notification:
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
// Set the info for the view that shows in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
You can see the pending intent takes a normal intent "new Intent(this, SecondActivity.class" so if you want specific behaviour (like bringing to the top instead of starting a new activity. Add the flags like you would normally to this intent. i.e. FLAG_ACTIVITY_REORDER_TO_FRONT (something like that)
Since platform version 11, you can build a notification using Notification.Builder. The v4 support library has an equivalent class NotificationCompat.Builder.
You can't change the Activity once you've sent the notification, but you can update the notification with a new Intent. When you create the PendingIntent, use the flag FLAG_CANCEL_CURRENT. When you send the new notification, use the ID of the existing notification when you call NotificationManager.notify().
Also, you should be careful how you start your app. The Status Bar Notifications guide tells you how to set up the back stack.
Hallo!
I have a question concerning notifications.
I want to create an application, that parses received SMS for a certain content. If the sms contains a certain content it is parsed and the data is extracted. A notification should be created and it should show up. When the user selects the notification, the application should be invoked with the extracted data. If the application is not running, it should be started. If it is already running, the instance should be displayed and processing the information.
To solve this I did the following:
A Broadcast receiver listens for SMS and parses them. To invoke the application I use the following code:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, tickerText, when); Bundle bundleNotification = creatingNotificationBundle(...);
Intent notificationIntent = new Intent(Intent.ACTION_MAIN); notificationIntent.setClassName(...); notificationIntent.putExtras(bundleNotification);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify((int) System.currentTimeMillis(), notification);
The Activity that I invoke processes the Intent in the onCreate(...) method. This works fine when the application is not running. When I already have an instance running, it is invoked and no data is passed (or I'm not overwriting the correct methods). I have seen the an onResume() is invoked. But this method doesn't have an intent as a parameter. What is the best idea to solve this problem?
I did an (in my opinion) very dirty workaround to get this work:
I created a temporary activity
I do the same code as above except that I invoke this new activity and set the flags notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) and notificationIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
this activity sends first a Broadcast and than an Intent both forwarding the data previously parsed.
Using this way the main application alwas gets the data. If it is already running it receives the broadcast and comes to front with the intent, if it is not running, it receives the data from the intent in the onCreate(...) method.
But I don't think that this is the correct solution for this problem, since it seams quite dirty to create a dummy activity just for redirecting data and doing nothing else.
Any ideas and suggestions would be highly appreciated.
Thanks
You probably want to override Activity#onNewIntent(android.content.Intent):
…when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.