In build notification bar in android - android

Any one can tell me how could i get inbuild notification listner in android. What all i want is if user put password on screen and if then some notification arrives. and if user click on notification, I want password field should be reset.

See http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification to create a local notification.
In the intent you use for the notification add an extra bit of data that you define (e.g. "ClearPassword" as a boolean of true).
In your activity, check for extras and your specific extra and if it is set then you can clear the field.

Why don't you just clear the password field in the onPause method of the activity? If the user clicks on the notification, the pending intent will launch a new activity (most in the cases from another application). When your activity is no longer the foreground activity, its onPause method will be called.

Related

how to show custom dialog box when android application receives notification

popup message feature like whats app, caller id like the true caller, I am trying to make the same thing.
I tried using a broadcast receiver but I do not know how to show the dialog box when a broadcast receives a notification.
a pop-up message like WhatsApp shows when a notification arrives on a phone.
Set mainactivity as a launcher screen. In the mainactivity before setContentView method calling check if the intent has extra parameters as per your requirement. If yes, then finish the current activity and start DialogActivity.

Building a task stack for activity started after Firebase Notification

Using Firebase Cloud Messaging, when the app is in the background and a message has arrived, the message goes to the system tray. When the user than clicks on the notification, the app gets launched and the launcher activity gets the message data in the Intent.
In my case, this notification is about some new results, so when pressed, I want to start a ResultsActivity.
In order to make this happen I do this in the OnStart of the LauncherActivity
:
Intent intent = getIntent();
String searchId = intent.getStringExtra("search_id");
if(searchId != null){
Intent resultsIntent = new Intent(LauncherActivity.this, ResultsActivity.class);
resultsIntent.putExtra(ResultsActivity.SEARCH_ID_EXTRA, searchId);
startActivity(resultsIntent);
}
This all works great.
The problem is now when clicking on the "up" arrow on the app bar, the app does not go to the parent activity that is defined in the manifest (which is not the launcher activity) but to the launcher activity. This is not surprising since the ResultActivity is started from the LauncherActivity, but this is not the wanted behavior. The wanted behavior is for the back arrow to send to the parent activity, which happens to be MainActivity.
I know there is the TaskStackBuilder for that kind of stuff, but I don't know how I can apply that pattern to my case here where I start the activity "normally" from another activity and not from some Notification Builder.
Is TaskStackBuilder the right solution here? If so, how can I change the code above to use it? if not, what is the right solution for this?
What I ended up doing is on the server side, with the firebase cloud messaging admin, instead of including a firebase_admin.messaging.Notification object in the firebase_admin.messaging.Message object I am then sending, I just put the notification title and text in the Message's data, and then build a notification by myself normally in MyFirebaseMessagingService. Since I'm now building the notification by myself I can add the TaskStackBuilder normally.
I guess this doesn't really answer the question of how to add a back stack when not using Notification.Builder, but it's probably a better solution anyway.

How to send user to another activity Push Notifications Android

I was reading and I can['t get to an answer, I want to send a push notification to my user, but when the user clicks open a custom intent, I don't want to do this for all the notifications, just for a couple. Let's say I want to send here
The value to open a custom intent, and then if I don't put anything there, the app will still open the default intent, I need this because I want to send the user sometimes to another activity
One of the way to achieve this is to always open the MainActivity and pass the information in your notification data(called as payload schema) to the MainActivity as an intent. In your payload schema you will pass in the information which will help you decide which activity to open. These information will be in form of key value pair. For example you can define a parameter action and this will contain an identifier for the activity that you want to open when the notification is clicked.
In your main activity you will have a logic to read this information from your notification schema and redirect it to appropriate activity. Something like this
//Main activity code
public void onResume() {
super.onResume();
if(action == "Activity1") {
//start activity 1;
}
}
If you dont pass anything in notification it will by default launch the main activity.

ObservableObject updateValue doesn't reach the right instance after startActivity

I have a basic Activity which mainly allows the user to change settings and save them. I also have a BroadcastReceiver which is launched on SMS_RECEIVED.
The main point of the app is to vibrate whenever a certain message is received until the user taps a button to make it stop. The activity is only there to allow the user to change settings and press the "Stop" button.
In my onReceive method (BroadcastReceiver), I get the content of the last message received and make the phone vibrate if the message is equal to a certain string. All of that is working perfectly, the problem is when I want to make it stop. Right now, I'm trying to make a "Stop" button appear in the Activity when the phone starts vibrating.
I understand that UI elements should remain in the Activity and so what I'm trying to do is communicate between the Activity and my BroadcastReceiver. I've found here how to do that with an Observer. The problem though is that I want the app to function at any time, even at boot time. It's very easy with a BroadcastReceiver but since it requires the Activity to be shown to allow the user to stop the vibration, I have to start the activity if it isn't started already.
So what I do is this:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("SMSReceived", true);
context.startActivity(i);
ObservableObject.getInstance().updateValue(true);
The problem is, when there is no instance launched, it creates a new one and sends the extra boolean correctly but the updateValue method doesn't seem to get called at all (due to the previous instance I suppose?) and inversely, when there is an instance launched (in the background) the extra boolean doesn't get passed and the updateValue method gets called correctly.
I suppose I could just launch the Activity on boot and immediately put it in the background but it could cause problems if the user closes the application, at which point it would simply stop working until the user started it again since the Observer would have no instance to send data to.
Do you guys have any idea of what I could do to solve my problem?
If it's not clear I can try to explain further.

Continuing the same activity when user clicks on Notification

i am making an application in which Broadcast listener is starting an activity and notification is displayed.
when a user clicks on home button it goes to the home screen.
But if user clicks on Notification icon then activity state is lost :( .
Please help me how to continue with my activity when user clicks on notification.
I don't display a notification if the application that is doing the notifying is in the foreground; I just update the UI of the activity and let it be self-evident.
Try this:
Intent resultIntent = this.getIntent();
It gets the started intent from main activity. Worked for me.
I suggest using an alert dialog for notifications link here. This will keep your notification on the same screen and in the same activity.
If you want to keep any information when you press home you may use android storage for that and here is the link for that. Storage will be able to hold data that you need from other activity classes.
I hope this helps you.
When building your Intent for the PendingIntent you send with the notification, add the flag Intent.FLAG_ACTIVITY_NEW_TASK.
If the activity is already on the history stack, it will be resumed and a call to onNewIntent() will be triggered. If it's not, the activity will be started with a blank slate.
Try this
Intent.FLAG_ACTIVITY_TASK_ON_HOME

Categories

Resources