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();
Related
I am trying to get new data on every push notification i receive,
I am passing new data with every new push notification then add this data into bundle and pass it to my activity,
The issue every time when i send push notification my activity always use previous data in bundle.
Here is my code,
Intent notificationIntent = new Intent(context, Myactivity.class);
Bundle b = new Bundle();
b.putString("post_id", newsletter_key);
b.putString("newsletter_title", message);
notificationIntent.putExtra("bundle_push", b);
//notificationIntent.putExtra("NotificationMessage", newsletter_key.toString());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// set intent so it does not start a new activity
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);
Any help ?
Thanks in advance.
Replace PendingIntent by below Code
PendingIntent intent= PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
reference - http://developer.android.com/reference/android/app/PendingIntent.html
I have a task in which I have to show Notification for an incoming message inside an activity using BroadcastReceiver with the address and content on the SMS. Now I have to click in order to insert the values in sms in database. When the task is finished i want it to clear from the activity. Is it possible to create notification for incoming sms inside an Activity?
You can Try This no generate your own Notification.
#SuppressWarnings("deprecation")
private static void generateNotification(Context context) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "New Contact Added",
when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent intent = PendingIntent.getActivity(context, iUniqueId,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, "New", 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(2, notification);
}
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 developing notification related things.Problem is click on button. I wrote the following notification related code :
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"New update received", when);
Intent notificationIntent = new Intent(context,
MessageReceivedActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle=new Bundle();
bundle.putString("post_title", message);
notificationIntent.putExtras(bundle);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "Agility Logistics", "New update received", intent);
// notification.number +=1;
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
If button is clicked, the button onclick notification is overriding all the other notifications.This is undesirable. So how can I show all notifications received without having them overwritten by button click notification ?