1. ホーム
  2. android

[解決済み] 通知をクリックした後にアプリケーションを開く

2022-07-08 10:13:52

質問

私のアプリには、以下のようなコードで通知があります。

//Notification Start

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

   int icon = R.drawable.n1; 
   CharSequence tickerText = "Call Blocker";
   long when = System.currentTimeMillis(); //now
   Notification notification = new Notification(icon, tickerText, when);
   Intent notificationIntent = new Intent(context, Main.class);
   PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

   Context context = getApplicationContext();
   CharSequence title = "Call Blocker";
   text = "Calls will be blocked while driving";

   notification.setLatestEventInfo(context, title, text, contentIntent);

   notification.flags |= Notification.FLAG_ONGOING_EVENT;
   notification.flags |= Notification.FLAG_SHOW_LIGHTS;
   notificationManager.notify(1, notification);

}

私の通知は非常によく発射されますが、私の問題は、通知センターで通知をクリックしたときに、私のアプリが起動しないことです。

基本的に、通知をクリックした後、何も起こりません。 通知をクリックした後、メイン アクティビティを開始するには、どうしたらよいでしょうか。 ありがとうございます。

どのように解決するのですか?

以下のコードを参照してください。私はそれを使用していて、それは私のHomeActivityを開いています。

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    Intent notificationIntent = new Intent(context, HomeActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);