How to make push notification without internet - android

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);
}

Related

Push notification android basic questions

I am about it implement push notification from a node.js-server into my Android/kotlin-app.
Therefore i have used pusher.com which was very easy to implement for basic notifications
But: I want to create more costumizable notification, like a large image etc.
All the samples I find is about how to create a notification in Android.
e.g. this works great:
notificationManager = NotificationManagerCompat.from(this);
val largeIcon = BitmapFactory.decodeResource(resources, R.drawable.large)
val activityIntent = Intent(this, MainActivity::class.java)
val contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0)
val notification: Notification = NotificationCompat.Builder(this, App.CHANNEL_1_ID)
.setContentText("test")
.setContentTitle("test")
.setSmallIcon(R.drawable.heizungan)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setLargeIcon(largeIcon)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setStyle(NotificationCompat.BigTextStyle()
.bigText("test")
.setBigContentTitle("test")
.setSummaryText("Heizung"))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setColor(Color.RED)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build()
notificationManager!!.notify(1, notification)
Using this in my app, i can create and show this notification.
But how do I push this?
How can I push some values to my device and then show this notification?
Thanks
You would need a cloud service to actually send the notification to your device.
An example would be Firebase Cloud Messaging
As milhamj has already said you can use Firebase Cloud Messaging
How to do this... (click)
Find my answer for android service
On this link you have php code on backend that you can rewrite in javascript or check this link for nodejs
and java code for Android app (MyFirebaseMessagingService)

How to prevent Android notifications from overwriting themselves?

I am using XMPP (smack) to create a messaging application and I am sending notifications whenever I receive a new message. The problem is that if I receive messages from two different users I can only see the last notification. How can I change it? Here is my code.
Intent thisIntent = new Intent(mApplicationContext, ChatActivity.class);
thisIntent.putExtra("EXTRA_CONTACT_JID",contactJid);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, thisIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(mApplicationContext);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.fab_bg_mini)
.setTicker("Hearty365")
.setContentTitle("New message")
.setContentText(" You received a new message from " + contactJid)
.setContentIntent(contentIntent)
.setContentInfo("Info");
if(!ChatActivity.active){
b.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND);
}
NotificationManager notificationManager = (NotificationManager) mApplicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
And as you can see I put an extra contactJid which is important to me. I need to set it in such a way that if a user clicks one notification its contactJid will be this and if another its contactJid will be another.
notificationManager.notify(1, b.build()); is your problem - you need to supply a unique identifier for this notification, as per the documentation:
If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
You are supplying the constant 1 for each notification, instead of a unique ID. I'd suggest using a hash of the contact JID (which I assume is a string):
notificationManager.notify(contactJid.hashCode(), b.build());

real time notification by JSON format in android

i have an application about football/soccer , i'm using API for get information about the matches, i need a way to make a real time notification when a goal is added in this API which have a JSON format .
If you have access to your back end web service code, look into Google cloud messaging(gcm) service, it is made precisely for this purpose. If it is not feasible to use gcm, you need to set a repeating alarm, but it will not be as accurate as gcm. But I highly recommend going for gcm. Here's a link for your reference. GCM dev docs
Another work around for the case where you don't have access to source code for back end is to develop a middle layer sort of web service that keeps polling your back end and uses gcm to alert the clients. This way atleast you won't be wasting user's system resources.
This is how you pop a notification:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[] { 200, 200, 200})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder.build()); //id is the notification id, if you use the same id, the notification will override the previous
In order to achieve the functionality,
Use a service to frequently(maybe once in 5 minutes) check information about the match.
If you have any Updates, show a notification to the user.
To show Notification:
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext()).setTicker("Ticker Text").setSmallIcon("Icon").setContentTitle("Title").setContentText("Content Text").setContentIntent(pi).setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
For more info use this link
here
What you really want to do is have a Receiver that will run and check for updates. For real time updates, I would use a Socket.
It pretty much depends on your server.

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

Sending notifications to all devices

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.

Categories

Resources