To generate push notification based on user current device location in android - android

I am able to get push notification from server but my app should receive push notification when user device location change in latitude and longitude.
Case 1:
I have to send device latitude and longitude values to server and it has to send push notification whenever user device current location change.(Moves from Place A to Place B to get notification) even if the app is running in background,app needs to configure with system OS to get push.
Case 2:
Iam able to move to activity while clicking on push notification but i need like if i have received two push notification from server clicking on first notification it has to take FirstActivity and clicking on second push notification take to secondActivity.
How to identify which push notification is to redirect to activity.
code to show push notification in GCMNotificationIntentService:
private void createNotification(String msg){
Log.d(TAG, "Preparing to send notification...: " + msg);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.offer, "GCM Notification", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, FirstActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, NOTIFICATION_ID,notificationIntent, 0);
notification.setLatestEventInfo(this, "Message From GCM", msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID = NOTIFICATION_ID + 1;
}

1.You need to create service which will be runing in background. Also you need somehow send notification to the telephone from your server and handle it. But it could be achived without server, you could just listen to LocationListener.OnLocationChanged
2.According to second question you can prepare right Intent before displaying notification, assign it to the notification, and after clicking it, you will be moved directly to this activity.
Intent intent = //prepare your intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentIntent(contentIntent);
notificationManager.notify(notificationId, builder.build());

Related

No title in Amazon SNS GCM push notification

I am using Amazon SNS console to send android mobile push notification. I did receive push notification but only the body message is displayed, the title is not displayed.Also when the notification was touched the app was not opened. I am using Amazon AWS SDK for Unity.
It would be great if you share some code, but as i understand you want to receive push with title and body message, so you have to retrieve it first.
When you receive push intent in your Service you should call intent.getExtras().getString("title"). And if you want to open app when you click on push notif you should set it up in your notification Intent.
Here is a simple example how to do it:
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty() && messageType != null) {
switch (messageType) {
case GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR:
Toast.makeText(this, GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR, Toast.LENGTH_SHORT).show();
break;
case GoogleCloudMessaging.MESSAGE_TYPE_DELETED:
Toast.makeText(this, GoogleCloudMessaging.MESSAGE_TYPE_DELETED, Toast.LENGTH_SHORT).show();
break;
case GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE: // retrieve your data here
Log.d("onHandleIntent", "Message= " + extras.getString("message"));
Log.d("onHandleIntent", "Title= " + extras.getString("title"));
sendNotification(extras.getString(message), extras.getString(title));
}
}
PushBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, String title) {
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class); // activity wich you want to open when click on push notification
notificationIntent.putExtra(NOTIFICATION_TAG, msg);
notificationIntent.putExtra(NOTIFICATION_TITLE_TAG, title);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle(title)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
As #Jarik said you would have to do that.
Also assuming youre facing the same problem i did, create a jar file whose class extends the gcm listener or whatever class the callbacks work with(dont remember the name right now, open their class file and you will find the java classes extending the gcm receivers), and then use intent for the notification touch that would open the application. Also this would let you use the title functionality. I found that somehow in amazon sns sdk class files, they have overwritten the title with a "" hence the title always seem to be blank.
Dont forget to put UnityPlayerProxyActivity in the Android manifest.

Get unseen android notifications

I have a service that shows a notification PendingIntent each time it receives a new GCM message. The problem is that the GCM messages can be of different kinds. And if many notifications leave unread, I want not to show them separately but in groups like:
you have 3 unread messages of type A
you have 2 unread messages of type B
you have 4 unread messages of type C
As far as I understand, to get this effect I need to have an access to unread/unseen notifications. Each time when I new notification comes I can check, if there is another unread message of this type, and then decide, whether I create a new notification or update an old one.
My question is: is there a way to see, which notifications are unseen and get access to them?
For any case this is my method to create a message; if an argument notificationId is 0 a new notification should be created. Else - updated.
private int sendNotification(String msg, Integer notificationId) {
Log.d(TAG, "sending message with text: "+msg);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int notification_id = notificationId==0?random.nextInt(9999 - 1000) + 1000:notificationId;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notification);
Intent intent = new Intent(this, MainActivity.class);
// Send data to NotificationView Class
intent.putExtra("text", msg);
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("escos")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(pending);
mBuilder.setContent(remoteViews);
remoteViews.setTextViewText(R.id.notiftext, msg);
remoteViews.setImageViewResource(R.id.notifim, R.drawable.ic_launcher);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager.notify(notification_id, notification);
return notification_id;
}
For different Notification strip (A, B, C etc.) in your status bar, use different NOTIFICATION_ID for building the Notification on basis of your defined type or collapse_key received from GCM.
For determining unread and read messages, use a local variable (counter) in Shared Preferences and increment it each time a specific type of Notification comes (on basis of defined type or collapse_key).
Then generate the Notification with that particular NOTIFICATION_ID as Notification with particular NOTIFICATION_ID can override each other. So You can override the previous Notification with Iterative Numbered text in New Notification.
As soon as user click on any Notification or particular Notification, clear the notification and reset the value of (counter) in Shared Preferences.
Edit1 : When you click on Notification with particular Pending Intent, then in that Activity use this code for removing all the Notifications generated from your app :
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
nMgr.cancelAll();
} catch (Exception e) {
e.printStackTrace();
}
Note : Do remember to add Try-Catch before you call cancelAll() as cancelAll() may not be supported by the device model and will generate
java.lang.SecurityException: Permission Denial
error.
Edit 2:
You can also use nMgr.cancel(NOTIFICATION_ID); to clear a specific notification, pass NOTIFICATION_ID to particular intent via extras and get the extras in that activity to cancel a particular notification.
And as you click on any notification it will be cleared from status bar unless you have not set .setAutoCancel(false) in your Notification Builder.

Update android notification adding new message to old message

My app sends notifications but the problem is that if there are some notification pending in the action bar, I want to update the message adding the new message to the old message.
For example, if the message is Hi!, and the device receive another message (Pamela) before the user has opened the old message, I want to show only one notification with the message Hi! Pamela.
My code for send notifications is:
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, msg, when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(getApplicationContext(),
FirstActivity.class);
notificationIntent.putExtra("message", msg);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int notifyID = 1;
PendingIntent intent = PendingIntent.getActivity(this, notifyID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Is it possible to get the old message of PendingIntent?
Thanks in advance.
One way to do this is to concatenate the string, by saving what is already displayed like, before the following is called for the first time:
notificationIntent.putExtra("message", msg);
//save "Hi" , concat "Pamela" update the notification with the same notification id
You save the string msg, then concatenate the string that you receive in the next notification. However, to identify, which notifications contain parts of String to be concatenated, is required in this case. Either you can set prefixes to messages or identify by notification id.
Not the ideal way to do it i feel, but can't think of anything else.
It's not possible to retrieve the existing notification from the NotificationManager. You need to keep track of it yourself independently of the notification, and save the unread messages somewhere else (maybe in a SQLite Database or SharedPreferences file).
You will need to determine if the user swipes away the notification, which means you will have to use the NotificationCompat.Builder class to create the notification. You can use the setDeleteIntent method to supply an Intent to trigger when the notification is swiped away, at which point you delete the old messages from wherever you are storing them so that a new message will show a notification with only the new message.

Android Show Notification only when Message arrives

In Android, I have implemented Google Cloud Notification, which notifies when any message arrives, but it stays even when message is there, it shows icon in notification bar immediatly after installing application, is there any way to only show the notification when message arrives and hide it once the user clicks on it ??
Here is my code :
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
//Intent notificationIntent = new Intent(context, MainActivity.class); //Open Activity
// set intent so it does not start a new activity
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.google.com")); //Open Link
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Without seeing where you are receiving messages and where you want to generate and cancel Notifications, it is very difficult to discern what you need to do to implement this, but I will give it a shot.
If you load the sample project for an Android Connected App Engine project (via GCM), you will notice that GCM data is received in a GCMBaseIntentService.
This class has a method you can override called onMessage() that will be called whenever you receive a GCM message. If you create the notification in this method, then your application will only generate a notification when a GCM message is received.
As for cancelling notifications- you can do so by calling the NotificationManager's cancel() method, passing the ID of the notification you want to cancel. Presumably you have some sort of Activity that displays messages to the user, and this would be a good place to cancel any outstanding Notifications relating to a specific message.

when to clear notification count of status notification in android

I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).

Categories

Resources