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を作成した理由を知っているようActivityで書かれたすべてのcommentsをお読みくださいActivity

次開きます。この?実行中のアプリの概要を知るために四角いボタンをクリックすると、Android OSは「最大化」をどのように達成しますか? –

+0

あなたが拡大することができるだった活動を監視する – HelloWorld

+0

どのような四角いボタン –

答えて

1

Activityを開いているときにNotificationをクリックすると、Activityが開きます。つまり、あなたのPendingIntentが、これは

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

関連する問題