【问题标题】:Android Phonegap app - Bringing activity to frontAndroid Phonegap 应用程序 - 将活动置于最前面
【发布时间】:2016-05-08 12:15:45
【问题描述】:

我正在自学如何在 Android 上使用 Phonegap 通知。虽然显示通知似乎是一个相当简单的过程

public void triggerTestNotification(String tag, int id,Context ctxt) 
{
  Intent notificationIntent = new Intent(context, PallActivity.class);
  notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
  Intent.FLAG_ACTIVITY_CLEAR_TOP);

  notificationIntent.setAction(Intent.ACTION_MAIN);
  notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  PendingIntent contentIntent = PendingIntent.getActivity(ctxt, 0,  
  notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Notification not = new Notification.Builder(ctxt)
  .setContentTitle("Title").setContentText("Title")
  .setTicker("Tick tock")
  .setAutoCancel(true) 
  .setContentIntent(contentIntent)
  .setSmallIcon(ctxt.getApplicationInfo().icon).build();
   NotificationManager notificationManager = (NotificationManager)    
   ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.notify(tag, id, not);
 }

我发现比较困难的是以下

  • 用户启动应用程序
  • 用户离开并开始做其他事情 - 应用程序在后台运行
  • 通知到达
  • 已显示
  • 用户点击通知。

此时应用应该回到前台。我以为我的意图代码

public class PallActivity extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
  finish();
  forceMainActivityReload();
 }

 private void forceMainActivityReload()
 {
  PackageManager pm = getPackageManager();
  Intent launchIntent =    
  pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());           
  startActivity(launchIntent);
 }

 @Override
 protected void onResume() 
 {
  super.onResume();
  final NotificationManager notificationManager = (NotificationManager) 
  this.getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.cancelAll();
 }

}

会处理这个,但它什么都不做。显然,我在这里遇到了问题 - 可能在 Intent 标志中。我非常感谢任何可能让我走上正轨的人

【问题讨论】:

  • 你为什么在onCreate中调用finish
  • 我把finish()放在那里是因为我在另一个插件中看到了它。但是,我已经尝试将finish() 的代码移到onCreate 的底部并一起删除。应用仍然没有回到前台。
  • 您能否说出 Pall 活动的目的是什么 - 我的意思是它会立即重定向到您应用的启动器活动。 PallActivity 不是您的启动器活动吗?你能包括你的清单吗?
  • 首先使用NotificationCompat.Builder 并从您的notificationIntent 中删除以下内容:notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

标签: android push-notification phonegap-plugins background-foreground


【解决方案1】:

从您的 oncreate 中删除 finish();,它会在您继续之前调用完成。

您打算开始第二个活动或至少将 PallActivity 视为第二个活动,而不是启动 PallActivity,似乎。您无需像在forceMainActivityReload(); 中那样启动它。我会从通知意图开始您希望开始的活动,而不是让问题变得更加复杂和混乱。

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

确保您使用的上下文是:

Context context = getApplicationContext();

并且,如 cmets 中所述,从通知中删除这些:

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

如果需要,这里有更多详细信息:

Resume an activity when clicked on a notification

Android: How to resume an App from a Notification?

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

How to make notification intent resume rather than making a new intent?

【讨论】:

  • 谢谢,Yvette - 特别感谢您提供的一系列有用的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多