Sending notifications to all devices - android

I have an app in which markers can be added to the map using the Google Maps API, I'm trying to send a notification to all devices with the app installed when a new marker is added, this works for the device that is currently using the app but not my other device which does not have the app loaded, is there something else I have to do to register it with other devices?
Here is the code for connecting to the server and adding the marker, which calls the showNotification method:
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://***.***.***.**/markerLocation/save.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
String msg = "Data entered successfully";
//The method call that makes the alert notification
ShowNotification(name);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
and here is the code for creating the alert:
public void ShowNotification(String name)
{
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GoogleMapsActivity.this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(GoogleMapsActivity.this, 0, intent, 0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(this)
.setContentTitle(name)
.setContentText(name + " has added a marker in your area")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(0, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}

First you will need a Push service to warn other devices of your new marker, then you will need a BroadCastReceiver to receive the push message and emit the Notification on all devices that received it, I would love to explain this and write some example code for you but its widely explained in Android Docus so why reinvent the wheel?
Look at this page, it has everything u need:
Google Cloud Messaging GCM

I think you are not grasping how the two notifications type functions. The way you would do this is by storing on your server the device id of all your users that have requested to receive the notifications. Then you initiate the notification for all devices from the server not from the app. Notification initiated from the app are only to display a message on the device outside of our app's UI
Take a look at this: https://developer.android.com/google/gcm/index.html

What you need is some kind of support for push notifications, an obvious choice for Android is Google Cloud Messaging (GCM).
Since you have a server available, you could tailor the server-side yourself for managing which devices that receive the notifications (plenty of tutorials out there).
If you are in a hurry and just want to get stuff working, you can use parse.com. They allow you to send push messages to 1 mil unique devices (through GCM) for free. The upside here is that they make it easier to setup and filter which devices that should receive the notifications.

Related

How to prevent android quick reply notification from being stuck in loading?

My app uses the Android N's new quick-reply feature to quickly take notes from a fixed notification, without having the user to open an activity.
However, I would like the notification to be reset to its initial state after the user sends the quick reply message, without having to dismiss the notification.
Here is my code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createChannel() : "").setSmallIcon(android.R.mipmap.sym_def_app_icon).setContentTitle("My Awesome App").setContentText("Doing some work...").setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
android.support.v4.app.RemoteInput remoteInput = new RemoteInput.Builder(BookmarkCreatorReceiver.TXT_REPLY).setLabel("Reply").build();
Intent replyIntent = new Intent(this, BookmarkCreatorReceiver.class);
PendingIntent replyPendingIntent = PendingIntent.getBroadcast(this, 0, replyIntent, 0);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_email, "Bookmark", replyPendingIntent).addRemoteInput(remoteInput).build();
NotificationCompat.Action actionQuick = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_email, "Quick", replyPendingIntent).build();
builder.addAction(action);
builder.addAction(actionQuick);
}
startForeground(NOTIFICATION_ID, builder.build());
The problem is that once the user sends a message and the Broadcast receiver is called, the notification stays in the loading state like this:
How can I remove the loading message, without having to discard the notification?
Per the Notifications in Android N blog post:
After you’ve processed the text, you must update the notification by calling notify() with the same id and tag (if used). This is the trigger which hides the Direct Reply UI and should be used as a technique to confirm to the user that their reply was received and processed correctly.
Also note:
For most templates, this should involve using the new setRemoteInputHistory() method which appends the reply to the bottom of the notification.
This ensures that users see the text they entered was properly handled.

how to send images in push notifications (GCM) android?

I am new to push notifications concept. So i am learning about push notifications. How we get multi-line text in push notifications.
Recently i saw images in push notifications too. how we get that type of push notifications in android. Please any one suggest me or provide links. Thank you in advance.
I tried but not getting multi line notification. This is not working for older versions .
Code:
PendingIntent p= PendingIntent.getActivity(context, 3456, in, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b=new NotificationCompat.Builder(context);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.reload_logo)
.setTicker("Post Paid bill")
.setContentTitle("Reload.in")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
//.setContentText("Rs.has to be paidjhgfhdjgf dhfhh hfdufgjhbj fhgjfg bfdjgjfg jfjb ndjfd d vdgfvhdgf")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(p)
.setContentInfo("Info")
.setContentText(message).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
For showing multiple text in notification you can use below code.
For Jelly Bean and higher you can use an expandable notification. The easiest way is to use the NotificationCompat.BigTextStyle for your notification.Use below code.
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(getString(R.string.title));
bigTextStyle.bigText(getString(R.string.long_explanation));
mBuilder.setStyle(bigTextStyle);
You can not send images in Push notifications. There is data limit you can send in push notification.
Android
The message size limit in GCM is 4 kbytes.
https://developer.android.com/google/gcm/server.html#params
Instead of that you have to send Url of image in server then on Receiver side at time of receiving push notification you have fetch image from server and then you have to display image in notification.
EDIT
For showing large image in notification refer this.
On your server side; In the message body you can send key value data.For multiline text use long notification to show the message. https://developer.android.com/guide/topics/ui/notifiers/notifications.htmlFor image you need to ask for a url from service and fetch that image and show accordingly.http://developer.android.com/reference/android/app/Notification.BigPictureStyle.html

How to make push notification without internet

me and my colleague are trying to show "push notification" message in mobile device. Colleague is telling that it can´t be done without Google Cloud Messaging but I think - why use any server for that?
We want something like this:
How our application will work is:
- user has app on background
- ajax request is made (request to our server)
- server response is: You have 1 new message
- message is showed in top strip on mobile.
Of course, the message can be showed without internet.. My GF had mobile app "Pou"... when he pooped notification was displayed... Just I dont get it why to use any Google service for that?
Can somebody direct me pls?
You should use Notification to show "push notification".
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setTicker(getString(R.string.notification_ticker_text))
.setContentText(getString(R.string.notification_content_text))
.setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, 0))
.setWhen(System.currentTimeMillis())
.setContentTitle(getString(R.string.app_name))
.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
((NotificationManager) this.getSystemService(NOTIFICATION_SERVICE)).notify(0, notification);
}

Android push notification opening a custom page

my question for you is the following: I have a web app written in HTML5, wrapped as a native Android app in order to use Google Push Notifications. Because my app is using many notifications for different reasons, I want to be able to say each time a notification is received, which page to be open, like adding a 'href' in the notification intent. Is this possible?
If I wasn't clear enough please let me know.
Thanks
You can define your own notification message content. The Message builder from Google supports key value pairs to be set by the sender of the notification.
See http://developer.android.com/reference/com/google/android/gcm/server/Message.html
Example:
Message message = new Message.Builder()
.addData("link1", "http://mypage1.com")
.addData("link2", "http://mypage2.com")
.build();
When you create the notification, use setContentIntent() to attach an Intent that has been constructed to visit the right webpage:
// assuming <this> is an Activity or other Context
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(yourUrl));
PendingIntent urlPendingIntent = PendingIntent.getActivity(this, 0 urlIntent, 0);
Notification.Builder b = new Notification.Builder(this)
.setSmallIcon(...).setContentTitle(...).setContentText(...) // etc.
.setContentIntent(urlPendingIntent);
NotificationManager noMan
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noMan.notify(ID, b.build());
If you expect to have more than one of these in the notification panel at a time:
Reconsider. It's spammy to post more than one notification.
If you must, you'll need a separate ID (or separate tag) for each.

how to send push Notifications to other device in android?

Using this code, i am able to send a notification to my own device.
Intent intent = new Intent(getApplicationContext(), ContactDonor.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
//display text
String body = "Please Click on this to accept!";
String title = bloodgroup+" Required";
Notification n = new Notification(R.drawable.ic_launcher, body , System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
finish();
But now i have a screen where a person's details are displayed like:
Name: ...
email: ...
, and there is a message box and a Request Button , on the click of that button, he should receive a notification with that particular message. How can this particular thing be done?
It cannot be implemented by using PUSH notifications. PUSH notification is useful when there is server-client comm is implemented where server notifies the client about the event that has occurred on server side.
What you are trying to implement indirectly is server-client architecture, where your device will act as server. If you mould your current architecture to server-client you will be able to send notification to other device. In this case also, you don't need PUSH notifications, it will be a simple server-client communication.
For more info on PUSH please see: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html#c2dm_sendmessage
You can also send SMS, but that will not solve your problem. According to me, there is no other solution that you can apply to send notification.

Categories

Resources