Как go к активности, нажав на уведомление? - PullRequest
1 голос
/ 02 апреля 2020

Я работаю над приложением, которое показывает уведомления и звуки уведомлений пользователю. Пользователь может выбрать, если он хочет видеть их с помощью 2 кнопок переключателя в настройках активности. Я хочу открыть посетитель активности, когда пользователь нажимает на уведомление. Я написал следующий код, но этого не произошло, что я должен написать вместо этого?

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.Set;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.example.myevents.R.drawable.notification;


public class Settings extends AppCompatActivity {
    Switch simpleswitch1;
    Switch simpleswitch2;
    private Notification notification;
    NotificationManager manager;
    Notification myNotication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
        simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
        simpleswitch1.setChecked(false);
        simpleswitch2.setChecked(false);
        simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @TargetApi(Build.VERSION_CODES.O)
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){{
                    int notifyID = 1;
                    String CHANNEL_ID = "my_channel_01";// The id of the channel.
                    CharSequence name = "channel 1";// The user-visible name of the channel.
                    int importance = NotificationManager.IMPORTANCE_HIGH;
                    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

                    Intent intent = new Intent(Settings.this, Visitor.class);
                    intent.putExtra("yourpackage.notifyId", notifyID);
                    PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);


// Create a notification and set the notification channel.
                    Notification notification =
                            new NotificationCompat.Builder(Settings.this)
                                    .setSmallIcon(R.drawable.notification)
                                    .setContentTitle("TITLE")
                                    .setContentText("TEXT")
                                    .setChannelId(CHANNEL_ID).build();



                    NotificationManager mNotificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.createNotificationChannel(mChannel);

// Issue the notification.
                    mNotificationManager.notify(notifyID , notification);



                }

1 Ответ

1 голос
/ 02 апреля 2020

Как описано в документации Android, вам нужно установить намерение, которое будет срабатывать, когда пользователь нажимает на уведомление на вашем сборщике:

NotificationCompat.Builder(Settings.this)
     .setSmallIcon(R.drawable.notification)
     .setContentTitle("My Events")
     .setContentText("Νέα εκδήλωση κοντά σας!")
     .setContentIntent(pIntent)
     .setChannelId(CHANNEL_ID).build();

См .: https://developer.android.com/training/notify-user/build-notification#click

...