从通知栏启动应用程序

时间:2012-11-12 10:52:45

标签: android cordova android-notifications android-notification-bar

我正在使用phonegap开发一个Android应用程序。我想从通知栏启动应用程序。虽然这可能是一个重复的问题,但似乎没有什么对我有用;我尝试过的链接很少。

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

从这个链接开发的独立应用程序正在工作但是当我与我的真实项目集成时,点击通知栏什么也没做,下面是我的代码

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

先谢谢,

Nanashi

2 个答案:

答案 0 :(得分:1)

我使用NotificationCompat.Builder因为它更容易实现。

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

答案 1 :(得分:0)

使用setContentIntent并将挂起的intent作为参数传递。

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);
相关问题