Android notification, when pushed should open that activity - java

Im having problems with notifications in android. Im making a chat client.
when my client tells me that someone is chatting with me from a previous chat, and I push it, it opens up a new instance of that chat. I want it to work like the facebook mesenger, that is when I push the notification it opens up the chat for the person which is trying to speak to me, instead of an empty chat window.
regards.

In your Notification you can set a Intent:
Notification n;
n.setContentIntent(intent)
In your intent you will set the activity that will be started and also you can put extras, like an ID of the conversation or something that can be identified latter.
Because when you start this new chat activity on onCreate() you need to check if there is extras, take the ID for example and search for your conversation history and write all your logic.

Related

Android Q - Privacy change activity start through NFC

https://developer.android.com/preview/privacy/background-activity-starts
From this, it results that my payment app, which shows an Activity when a NFC transaction is performed, will not be able anymore to show anything to the user.
Has anyone have a clue what would be the new approach ?
Thanks!
I currently use the NFC service and it starts an Activity intent.
Intent intent = new Intent(mApplicationContext, PaymentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mApplicationContext.startActivity(intent);
The Activity should be shown. It works now, but from Android Q, it won't
According to the link, if you are having a HostApduService, then your app should work the same in Android Q.
If that is not your case, the simplest work around is to get "Draw over other apps" permissions. You can open activities if the app has been granted the SYSTEM_ALERT_WINDOW permission by the user. I have tested this and working.
Technically, you are showing something on top of other apps without user's interaction, so this might be the right way to go.

Prevent app from running on two instances

I just recently added the capability of my app to check for updates on our local server (this app is not published in the Google Play store. It's going to be used internally and there is no internet connection where it will be used. Don't ask. That's how it is :) ). I keep track of updates by checking a certain table in SQL Server and if my app's version is lower than what is indicated in this table, I download the new APK from an internal website (LAN only) then install the APK. I also have another application in the device that listens for PACKAGE_ADDED broadcasts. I can capture the broadcast successfully.
The problem is, after installation, the broadcast receiver starts the app by calling the following.
public class PackageInstalledBroadcastReceiver extends BroadcastReceiver {
private final String MY_PACKAGE_NAME = "com.company.packagename";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
String packageName = intent.getData().getSchemeSpecificPart();
if (packageName.equalsIgnoreCase(MY_PACKAGE_NAME)) {
Intent i = new Intent();
i.setClassName(MY_PACKAGE_NAME, MY_PACKAGE_NAME + ".LoginActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
But it takes a long time for the app to start. The user might think that nothing is happening, so he/she can start the app manually by clicking the app's icon. If the user clicks the app's icon, the app starts immediately. After a while, the activity that the broadcast receiver started is also opened. So now, I end up with two instances of my app in the same activity (LoginActivity). I can say that this is the case, because if I press the Back key on the device from the LoginActivity, I still end up in another LoginActivity and if I press the Back key again, I end up on the device's desktop.
You have two problem in your question:
The fist, why your BroadcastReceiver take a long time to start your activity.
It have not enough information for stoving this.
The second your want to make your activity have a single instance.
Android provide a way to do that:
Step one: Visit your application androidmanifest file
Step two: Find your activity declaration.
Step there: Add the following property android:launchMode = "singleInstance"
The reference here:
P/s: If you could provide my some more information of your fist problem. Please create a new question. Hope you resolve it.

Android, killing an Activity from another Activity from GCM message

I've currently wrote an app that receives a GCM message and stores it in a database then creates an alertDialog to show above any app what the new message is, the only problem i have is if a new message is received before the current alertDialog is closed you don't get to see the new message, if i sit and close each message its fine.
So i think what ive been trying is to ask 'is the alertDialog showing...if not show the message, if its already showing close it and open a new one with the new message'.
Does this sound feasible?
Cheers
Mark
I would recommend using a notification rather than an alert dialog for showing incoming messages. The NotificationManager lets you set an id and tag when you post a notification. Then if you later post one with the same id and tag, the existing one will be updated.
On Lollipop (and newer) devices you can even get a similar effect to what you describe with the notification coming up on top of the current app using heads up notifications: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

check automatically for new message android

I am trying to create a simple android messaging app. So basically let us assume that user A sends a message to user B. I would like that user B receives a notification when the message is received. I know how to create the notification and all. But basically I would like to know how the user B constantly checks if a new message has been received even when he is out of the app, and then that would trigger the notification and subsequent actions.
Thank you
You have to setup an unbound background service.
With this you can constantly make pull-requests to your server or get push-notifications from your server and display notifications.
https://developer.android.com/training/run-background-service/create-service.html
You can use Event Bus lib for this purpose. When new message will be received it will create and event and then you can receive that event event and do other operations.
If you are talking about text messages then you will have to create a BroadcastReceiver
http://developer.android.com/reference/android/content/BroadcastReceiver.html
You can see my answer here for SMS Receiver
Verify sms text message by app before it displayed to user
Don't forget to give permissions in your app's manifest.
Hope it helps.

Sending email through intent without having to press send button

Currently I have a button that when pushed calls the Intent below.
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sharingIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] { toString });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,
"New Files from Safe Storage");
sharingIntent.setType("text/*");
startActivity(sharingIntent);
This intent then uses the default share activity to share the email with my attached file (which i took out for this example). When this code goes off it opens the gmail activity for me, but i still need to push the send button even though everything is filled in. Is there a way to make this instead just send automatically without showing the user the activity and having them forced to push "Send"?
Have a look on the following link, there is an answer for your question.
Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)

Categories

Resources