Android-Frage zu Notifications und Actions

Moin,

    public void setMyAlarm() {
        // Create an explicit intent for an Activity in your app
        Intent intent1 = new Intent(this, MyReceiver.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 0, intent1, 0);

        //Intent intent2 = new Intent(this, MyReceiver.class);
        //intent2.setAction(Intent.ACTION_VIEW);
        //PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0, intent2, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, default_notification_channel_id);
        builder.setContentTitle(derNameDerApp);
        builder.setContentText(derText);
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(derBigText));
        builder.setSmallIcon(R.drawable.ic_launcher_foreground);
        // Set the intent that will fire when the user taps the notification
        builder.setContentIntent(pendingIntent1);
        //builder.addAction(R.drawable.ic_launcher_foreground, "(+1)", pendingIntent2);
        builder.setAutoCancel(true);
        builder.setChannelId(NOTIFICATION_CHANNEL_ID);

        Intent intent3 = new Intent(this, MyReceiver.class);
        intent3.putExtra(MyReceiver.NOTIFICATION_ID, 1);
        intent3.putExtra(MyReceiver.NOTIFICATION, builder.build());
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, next, pendingIntent3);
    }

die Notification funktioniert bereits! Aber wenn man A auf die Notification „tabbt“, soll

  1. sich die Notification schließen (funktioniert bereits),
  2. sich die App/MainActivity öffnen (funktioniert noch nicht),
    B auf einen zusätzlichen Tab (+1) „tabbt“, soll
  3. sich die Notification schließen (funktioniert bereits),
  4. sich die App/MainActivity öffnen (funktioniert noch nicht),
  5. eine Methode der App/MainActivity aufgerufen werden werden (funktioniert noch nicht).

(5.) ist ggfs. gar nicht möglich, da die MyReceiver.class die App nicht kennt, aber ich weiß es nicht genau.

Es klappt wohl indirekt: https://stackoverflow.com/questions/41312669/android-calling-methods-from-notification-action-button

Aber, wie sich die App öffnet, weiß ich noch nicht…

Lösung:
Die setMyAlarm Methode:

    public void setMyAlarm() {
        Context context = this;

        // Intent for clearing notification and open app
        Intent intent1 = new Intent(context, MainActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, intent1, 0);

        // Intent for tab action 1 (plusOneAction)
        Intent intent2 = new Intent(context, MyReceiver.class);
        intent2.setAction("plusOneAction");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        // Notification...
        Notification notification = new NotificationCompat.Builder(context, MyReceiver.DEFAULT_NOTIFICATION_CHANNEL_ID)
                .setContentTitle("MyRauchBot1")
                .setContentText("Du darfst eine rauchen!")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Du darfst eine rauchen! .... "))
                .setContentIntent(pendingIntent1)
                .addAction(R.drawable.ic_launcher_foreground, "1 geraucht (+1)", pendingIntent2)
                .setAutoCancel(true)
                .setChannelId(MyReceiver.NOTIFICATION_CHANNEL_ID)
                .build();

        // Intent for broadcasting
        Intent intent3 = new Intent(context, MyReceiver.class);
        intent3.putExtra(MyReceiver.NOTIFICATION_ID, 0);
        intent3.putExtra(MyReceiver.NOTIFICATION, notification);
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, next, pendingIntent3);
    }

intent2.setAction( ist wichtig, intent2.putExtra( funktioniert nämlich nicht, wie wir später sehen.
Der BroadcastReceiver:

public class MyReceiver extends BroadcastReceiver {
    public static final String NOTIFICATION_CHANNEL_ID = "9996";
    public static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "default";
    public static final String NOTIFICATION_ID = "notification-id";
    public static final String NOTIFICATION = "notification";

    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //Toast.makeText(context, "action=" + action, Toast.LENGTH_SHORT).show();
        if ("plusOneAction".equals(action)) {
            File f2 = new File(context.getCacheDir(), "plusOneAction1.txt");
            try (FileOutputStream fos = new FileOutputStream(f2)) {
                fos.write(1);
            } catch (IOException ignored) {
            }
            Intent i = new Intent();
            i.setClassName("com.example.myrauchbot1", "com.example.myrauchbot1.MainActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = intent.getParcelableExtra(NOTIFICATION);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_DEFAULT);
                assert notificationManager != null;
                notificationManager.createNotificationChannel(notificationChannel);
            }
            int id = intent.getIntExtra(NOTIFICATION_ID, 0);
            assert notificationManager != null;
            notificationManager.notify(id, notification);
        }
    }
}

Die onCreate der MainActivity:

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

        setContentView(R.layout.activity_main);

        //...

        File f2 = new File(getCacheDir(), "plusOneAction1.txt");
        if (f2.exists() && f2.delete()) {
            b3click(null);
        }

        //...
    }

b3click() ist nun die Methode in der MainActivity, die aufgerufen werden soll.