从 Android 中的通知打开 MainActivity

Openning MainActivity from Notification in Android

我想从通知中打开我的应用 Mainactivity,但不知道在哪里声明 builder.setContentIntent() 的意图 这是我使用的代码: ReminderBroadcast.java

public class ReminderBroadcast  extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyme")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Title for Notification")
                .setContentText("This notification is Working")
                .setPriority(NotificationCompat.PRIORITY_HIGH);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(200, builder.build());
    }
}

MainActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createNotificationChannel();
        
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Reminder set", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, ReminderBroadcast.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent,0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                long timeAtClick = System.currentTimeMillis();
                long fiveSeconds = 5 * 1000;
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        timeAtClick + fiveSeconds, pendingIntent);
            }
        });
    }
    private void createNotificationChannel(){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            CharSequence name = "ReminderChannel";
            String description = "This is used for channel";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel("notifyme" , name , importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

你必须使用 PendingIntent

 Intent activityIntent = new Intent(getApplicationContext(), MainActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotification")
            .setContentTitle(title)
            .setSmallIcon(R.drawable.noti)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentText(message);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(200, builder.build());