I'm creating an application that use Google GCM Service.
So I defined this function:
#Override
protected void onMessage(Context context, Intent intent)
onMessage calls a function, this is part of code:
Intent notificationIntent = new Intent(context,ShowMess.class);
notificationIntent.putExtra("id",current_id);
notificationIntent.putExtra("cat", cat);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(id, notification);
If I send a message to myself (with AsyncTask) but close fastly the applicaiton (Back Button) next messages will not launch activity (ShowMEss.class) Why in your opinion?
Bye
Thanks
Your above code has not registered the notification. Are you missing following part in your code?
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
Related
I have create an application to run connect to HC-06 module.and also there is brodcastlistener which listen to specific SMS and generate notification from my app.when I clicked the notification then it should load application activity.But my app always run while connect to bluetooth.But when click on notification i want to start an activity in same application .how could I do this??
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
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;
notificationManager.notify(0, notification);
I am trying to pass value of my push notification to my activity. But it always returns null.
Here is how I am trying to pass extra string:
Intent notificationIntent = new Intent(context, Register.class);
notificationIntent.putExtra("NotificationMessage", new_key.toString());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_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.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Here is my activity where I am trying to get this extra string.
Intent intent = getIntent();
String msg = intent.getStringExtra("NotificationMessage");
Toast.makeText(this, "Hello"+msg, Toast.LENGTH_LONG).show();
This toast always show me " Hello null ". Can anyone please help?
Thanks in advance.
Add PendingIntent to your notification via .setContentIntent(). It will trigger intent when notification is clicked.
Notification notification = new NotificationCompat.Builder(context)
...
.setContentIntent(intent)
...
.build();
I done an android application using Phonegap and Java. I got notification successfully in application , after clicking notification app is start from background.I want to reload my Index.html file after notification click. How can i handle this issue ?
Please help
thanks in advance.
thanks mit, here is my generate notification 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);
notificationIntent.putExtra("data1", "data1");
MainActivity ObjMain=new MainActivity();
ObjMain.sendJavascript("TestFunc");
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent contentIntent =PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 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;
if (message!="")
{
notificationManager.notify(0, notification);
}
}
I am making an app which requires me to send multiple push notifications to users which has distinct values which will be shows when user click on them from status bar.
I am making the intent using the below code
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.when = when;
notification.icon = icon;
Intent notificationIntent = new Intent(context,RecentStoryPage.class);
notificationIntent.putExtra("id", id_leader);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("message", message);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, "", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(Integer.parseInt(id), notification);
When I send single notification, this does not make any issues however whenever I make multiple notifications, whichever notification I click on, it always opens with the recent value.
I got my mistake, I made this line error full
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
which I should have made to
PendingIntent intent = PendingIntent.getActivity(context, Integer.parseInt(id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
I am using the following code for creating a notification in Android. But its working fine in some devices. But when I checked in Samsung Grand, it crashes while receiving the notification. Android version 4.1.2 ( both devices )
private static void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = "From Application";
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("message", message);
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;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, notification);
}
Any idea ? I am new to Android..
Thanks in advance