check automatically for new message android - java

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.

Related

Is there a way to delete push notification data message sent by FCM?

In my application, I am offering video and voice calls between users. I'm using Firebase Realtime database and FCM as well to do so.
Here is the data that my notification is delivering:
data: {
channel_id: channel_id,
user_id: user_id
}
In my FirebaseMessagingService class, I retrieve the data and open the Video/Call activity like so:
if(remoteMessage.getData().size > 0) {
Intent intent = new Intent(this, VideoCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("channel_id", remoteMessage.getData().get("channel_id"));
intent.putExtra("user_id", remoteMessage.getData().get("user_id"));
getApplicationContext().startActivity(intent);
}
Everything is working perfectly even when the application is in background. However, I'm facing this issue:
If an user didn't have internet connection when he was called, when he connects to internet his phone will automatically start the VideoCallActivity even if the call has been aborted (by the caller) and the notification removed from Firebase real time database.
So, please is there a way to cancel sent notifications, or know the delivery status of those notifications, or another way around to do so? That will allow me to add the list of missed calls for users.
Thanks!
In a scenario like this, I know of two options:
Send another FCM message when the call is cancelled
Only use FCM to send a tickle
Send another FCM message when the call is cancelled
One of the properties you can give an FCM message is an collapse_key. When you send a message with a collapse_key, that message replaces any previous message with the same collapse_key value.
You can use this to send a "nope, forget about it" message, when the user cancels the call.
Only use FCM to send a tickle
Alternatively you can have the FCM message just be a so-called tickle: an almost empty message, that just tells the app to wake up and go check its queue of incoming messages in the database.
That way: if the call is still in the database, the app can pick it up. But if there is no call in the database, it can just do nothing.
You'll want to do this type of database interaction in a service, so that it can run when the app is backgrounded.
Giving an expiration time is feasible as suggested in the documentation
Check Here
Setting the lifespan of a message
On Android and Web/JavaScript, you can specify the maximum lifespan of a message. The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message. Requests that don't contain this field default to the maximum period of four weeks.
Here are some possible uses for this feature:
Video chat incoming calls
Expiring invitation events
Calendar events

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

how to trigger a gcm to send a push notification to android device(using java server)

I am very new to java server side development, i have followed this link [http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/][1] and successfully implemented GCM with my android device, the problem is dont know how to trigger the GCM server while the content is updated in my db,i need to notify each and every update of my db to the user, am i need to watch the db using timer task something like that or is there any default solution to keep track of db ?
My Server side code :
regId = "my registration id";
String userMessage = request.getParameter("message");
Sender sender = new Sender(GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(30)
.delayWhileIdle(true).addData(MESSAGE_KEY, userMessage).build();
result = sender.send(message, regId, 1);
have tried with many solution but till now not getting exact solution, Suggestion, ideas or related links are most welcome
Thanks in advance
Without knowing the specific functionality of your server and app, I can only offer a general solution.
Each process in your server that performs DB updates that have to be pushed to some Android devices via GCM can write the messages and the registration IDs to some kind of queue.
Then you can have another process (or processes) that would consume the queue of GCM messages, and send them to Google. These processes can handle failures (retry sending the messages in case of temporary failures), and update your database of registration IDs if they receive responses with canonical registration IDs or errors such as InvalidRegistration or NotRegistered.

Is it possible to send a message from one app to another?

I have a text messaging app that I want users to be able to text another app of mine on someone else's phone that will show up within the app. Printed on screen. I have one app that will send the message and one app on the receiving device that will display the message. How would I go about doing this?
The receiving app is an opengl app that will display the message in front of a 3d model from the sending app.
If someone could help me out or get me going in the right direction, I would greatly appreciate it. Thank you for your time.
EDIT: This is what I'm doing. I'm making a live wallpaper that women can put on their phone, a seperate "regular" app will allow the husband, fiance, etc to send a message to the lwp on the significant others phone that will display at the top half of the lwp screen.
public void sendSMS(String phoneNumber, String message)
{
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
You can do something like this for sending the sms.
Yes, it's possible.
Use SMSPopup as a working Android project you can use. It has the two parts you need: the sending part and the receiving part, and it's open source. Here it is on Google Play.
The only issue is that the user probably won't want those application messages intermingled with his normal SMS messages.
So you'll want to tag your application text messages with a unique keyword so that the receiving app knows it's a message for itself and no one else. And by tagging, I just mean to insert a unique keyword at the beginning of its subject line.
And your receiving app will have to register a BroadcastReceiver with a priority of 100 so that if it detects a message intended for itself, it can just delete it from the content provider and just do an AbortBroadcast so that the other text messaging apps don't beep for a message that wasn't meant for them in the first place.
In that sense, SMSPopup probably already does 90% of what you need. SMSPopup doesn't automatically delete the sms it receives, nor will it filter them on a particular keyword, but it must silently swallow them so that the normal notification system for sms doesn't get triggered (since SMSPopup provides its own notification popup interface instead).
Hi Steve we have RabbitMQServer to send messages between apps. We should configure the server and need to implement functionlities to send and receiving messages.
You may get details about RabbitMQ server at http://www.rabbitmq.com/.

Android SMS API

I know that the SMS content provider is not part of the public API (at least not documented), but if I understand correctly it's still possible to use many of the SMS features as long as you know how to use the API(?).
E.g it's pretty straightforward to insert an SMS into your inbox:
ContentValues values = new ContentValues();
values.put("address", "+457014921911");
contentResolver.insert(Uri.parse("content://sms"), values);
Unfortunately this does not trigger the standard "new-SMS-in-your-inbox" notification. Is it possible to trigger this manually?
Edit: AFAIK the "standard mail application (Messaging)" in Android is listening for incoming SMSes using the android.permission.RECEIVE_SMS permission. And then, when a new SMS has arrived, a status bar notification is inserted with a "special" notification id.
So one solution to my problem (stated above) could be to find, and send the correct broadcast intent; something like "NEW SMS HAS ARRIVED"-intent.
Edit: Downloaded a third party messaging application (chompsms) from Android market. This application satisfies my needs better. When i execute the code above the chompsms notice the new sms and shows the "standard status bar notification". So I would say that the standard Android Messaging application is not detecting sms properly? Or am I wrong?
Unfortunately the code responsible for these notifications is hidden in the messaging application. The class MessagingNotification has a static method updateAllNotifications that you could call using a PathClassLoader and reflection:
PathClassLoader c = new PathClassLoader("/system/app/Mms.apk", getClassLoader());
Class.forName("com.android.mms.util.ContactInfoCache", true, c)
.getMethod("init", Context.class).invoke(null, context);
Class.forName("com.android.mms.transaction.MessagingNotification", true, c)
.getMethod("updateAllNotifications", Context.class).invoke(null, context);
This is obviously a very bad idea for several reasons but I can't think of another way to do what you described.
Could you trigger a PUSH notification after the SMS?
Thread: Does Android support near real time push notification?
Maybe you should replace
content://sms
with
content://sms/inbox

Categories

Resources