How to send user to another activity Push Notifications Android - 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.

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.

Duplicate activity to launch many instances of same activity using unique identifier

I am building a chat client and chatting with every user opens same activity ChatActivity passing an username as intent extras like this one.
Intent.putExtra("user", username);
Where username being the one one you are chatting with. After extras are sent, ChatAcitivity opens up, handles the extra's username and work accordingly. Now, if i am chatting with multiple users at once, say user1 and user2, each time the activity restarts to handle the data. How can i preserve the chat with both the user and open up the already initialized activity so as to reduce load both to the server & user.
I want something to identify the intent that needs to be opened like chatting with user1 brings user1 ChatActivity to the front and so i can use:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_USER1ACTIVITY_BROUGHT_TO_FRONT_);
or something to call that up.
How can i do that??
I understand you want to have 'duplicate' instances of the ChatActivity, one for each user. You can use the FLAG_ACTIVITY_NEW_DOCUMENT flag, the document being a conversation with a specific user :
// the URI is used as a document ID
// a stack / activity with the same id will be reused
// and the intent passed to onNewIntent()
// otherwise a new activity is created
startIntent.setData(Uri.parse("http://my.domain.net/users/" + username));
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Please note that each conversation will have its own entry in the Recents screen (Overview screen). This may or may not be what you want.

In build notification bar in 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.

Activity opened twice

I have an application that uses Urban Airship for push notification. When a notification arrives and the user clicks on it, activity A in my application should open and do something.
I've installed the BroadcastReceiver as is shown in the docs, and it's almost working.
When my app is in the foreground I don't let the user see the notification at all, and just handle it automatically.
When my app is not running at all, the activity opens up just fine.
When my app is in the background (which always happens when A is the top activity), a second instance of Activity A is created.
This is, of course, a problem. I don't want two A activities, I just want one of them. Here's the relevant BroadcastReceiver code:
#Override
public void onReceive(Context ctx, Intent intent)
{
Log.i(tag, "Push notification received: " + intent.toString());
String action = intent.getAction();
int notificationId = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1);
if(action.equals(PushManager.ACTION_NOTIFICATION_OPENED))
{
Intent intentActivity = new Intent(ctx, ActivityA.class);
intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UAirship.shared().getApplicationContext().startActivity((intentActivity);
}
}
UPDATE:
I tried to bypass this bug by calling System.exit(0) when the user presses Back on Activity A. The process ended, but then it was restarted immediately! My BroadcastReceiver is not called again in the second instance. What's happening?
UPDATE 2:
#codeMagic asked for more information about the app and activity A.
This app lets its user review certain items and comment on them. Activity A is started when the app is launched. If the user's session isn't valid any more, a Login activity is started. Once the user logs in, activity A becomes active again. A only has a "No items to review" message and a "Try now" button.
When the user logs in, the server starts sending push notifications whenever a new item is available for review. When the app gets the notification, activity A accesses the server and gets the next item to review. The item is shown in activity B. Once the review is submitted to the server, activity B finishes and activity A is again the top activity.
The server knows when a user is reviewing an item (because activity A fetched it), and doesn't send push notifications until the review is submitted - meaning a notification can't come if the user isn't logged in or if the user is viewing activity B.
While I agree there is a subtle race condition here, it is not causing the problem I'm seeing - in testing I am 100% positive there's no race condition - the push notification is only sent after Activity A becomes active again.
The solution was to add a launchMode='singleTask' to the activity in AndroidManifest.xml . As a result, instead of a new activity, onNewIntent of the same activity instance is called.
You can use one of several Intent Flags. FLAG_ACTIVITY_REORDER_TO_FRONT being one of them. This will bring the Activity to the front of the stack if it is already in the stack and if not then it will create a new instance. I believe you will still need FLAG_ACTIVITY_NEW_TASK if you aren't calling it from an Activity
Intent.FLAG_ACTIVITY_CLEAR_TOP should also work. But this will clear any other Activities on the stack. It just depends on what other functionality you need. Look through the Intent Flags and see which of these will work best for you
There are multiple scenarios when this could happen. One of them can be handled this way. Please see my answer here: https://stackoverflow.com/a/44117025/2959575
Ok, two notes on this :
You can register a broadcast receiver via the manifest so it is independent of any parts of your app. and use a Singleton pattern (keep a static reference to your activity somewhere in your app) that way you can check if their is an activity viewing or not and process accordingly.
// your activity A
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
myActivityReference = this;
}
public void onPause() {
super.onPause();
if (isFinishing()) {
myActivityReference = null;
}
}
or you can keep everything as it is and use activity lunching modes flags in your manifest such as singleTop, singleInstance ... etc. take a look here android activity lunch modes

Categories

Resources