Reputation: 155
I am developing an android app using phonegap. I want to launch the app from the notification bar. Though this may be a duplicate question, but nothing seems to be working for me;few links that i have tried.
http://pilhuhn.blogspot.in/2010/12/pitfall-in-pendingintent-with-solution.html
Open android app from PUSH notification
re-open background application via notification item
stand alone app developed from this links are working but when i integrate with my real project tapping on the notification bar does nothing, below is my code
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, MyClass.class);
intent.putExtra("message", message);
intent.putExtra("shortMsg", shortMsg);
intent.putExtra("source", source);
intent.putExtra("phone", phone);
intent.putExtra("datetime", datetime);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
notificationManager.notify(0, notification);
MyClass.class Code,
Bundle extras = getIntent().getExtras();
if (extras != null)
{
System.out.println("in extras");
//Retrive data from the intent and store them in instance variables
this.message = extras.getString("message");
this.shortMsg = extras.getString("shortMsg");
this.source = extras.getString("source");
this.phone = extras.getString("phone");
this.datetime = extras.getString("datetime");
}
Thanks in Advance,
Nanashi
Upvotes: 0
Views: 2004
Reputation: 31
Use setContentIntent and pass the pending intent as an argument.
Intent intent = new Intent(context, MyClass.class);
intent.putExtra("message", message);
intent.putExtra("shortMsg", shortMsg);
intent.putExtra("source", source);
intent.putExtra("phone", phone);
intent.putExtra("datetime", datetime);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
mNotification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setContentIntent(pintent)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pintent)
.build();
notificationManager.notify(0, mNotification);
Upvotes: 0
Reputation: 17129
I use NotificationCompat.Builder
because it is much easier to implement.
Intent intent = new Intent(context, Main.class);
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0);
mNotification = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setContentIntent(pintent)
.setSmallIcon(R.drawable.ic_notification)
.setWhen(System.currentTimeMillis())
.setAutoCancel(cancellable ? true : false)
.setOngoing(cancellable ? false : true)
.build();
notificationManager.notify(0, mNotification);
Upvotes: 1