0

我已经连接到Google Cloud Messaging,并在收到内容时显示通知。 我想在用户点击通知时“最大化”我的应用程序。即显示与我的应用程序相关的最新活动。或者,如果应用尚未开始,我想开始它的主要活动。当用户点击Android中的通知时显示应用

如果应用程序已经打开,则不必创建最后一个活动的新实例。

我该如何做到这一点?

我见过很多类似的问题,但所有的答案似乎都想指定活动类,我不知道,因为我不知道上一次显示哪个活动。

有没有解决这个看似简单的任务?

我的代码看起来像这样的时刻:

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.icon) 
      .setContentTitle("foo") 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)); 


    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

但它无法正常工作。

+0

使用应用程序的registerLifecycleCallbacks来监视活动 –

+0

你能否扩大其活动最后这个?点击方形按钮以获得运行应用程序的概述时,Android OS如何实现“最大化”? – HelloWorld

+0

呃什么方形按钮 –

回答

1

当您打开Activity而点击Notification只需打开以下Activity。即你的PendingIntent将打开下面Activity

请阅读所有的comments写在Activity,这样你就知道为什么这已经创造了

public class NotificationHandlerActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //deep linking - resuming app code 
     if (isTaskRoot()) { 
      // This Activity is the only Activity, so 
      // the app wasn't running. So start the app from the 
      // beginning (redirect to MainActivity) 
     } else { 
      // App was already running, so just finish, which will drop the user 
      // in to the activity that was at the top of the task stack 
      Intent intent = getIntent(); 
      Uri data = intent.getData(); 
      //you can put your extra's if any 
      finish(); 
     } 
    } 
} 
+1

太棒了!正是我需要的:)聪明! – HelloWorld

相关问题