0

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

Community
  • 1
  • 1
Nanashi
  • 155
  • 2
  • 14

2 Answers2

1

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);
valerybodak
  • 4,195
  • 2
  • 42
  • 53
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • i dont think u can put any key-value pairs in pending intents. i.e. intent.putExtra("message",message); and what is this line NotificationCompat.Builder(context); is there a class by that name??? – Nanashi Nov 12 '12 at 11:25
  • Yeah you're right about key-value pairs. Oversight from my part. `NotificationCompat.Builder(context)` is in the support library. – Binoy Babu Nov 12 '12 at 11:28
0

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