0

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

http://pilhuhn.blogspot.in/2010/12/pitfall-in-pendingintent-with-solution.html

从 PUSH 通知中打开 android 应用

通过通知项重新打开后台应用程序

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

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 代码,

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

提前致谢,

七无

4

2 回答 2

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);
于 2012-11-12T11:01:56.373 回答
0

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

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);
于 2016-05-10T06:53:32.760 回答