Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过单击通知转到活动?_Java_Android_Android Activity_Notifications_Click - Fatal编程技术网

Java 如何通过单击通知转到活动?

Java 如何通过单击通知转到活动?,java,android,android-activity,notifications,click,Java,Android,Android Activity,Notifications,Click,我正在开发一个向用户显示通知和通知声音的应用程序。用户可以通过“设置”活动上的两个开关按钮选择是否要查看它们。我想在用户单击通知时打开活动访问者。我写了下面的代码,但这没有发生,我应该写什么来代替 import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationChannel; i

我正在开发一个向用户显示通知和通知声音的应用程序。用户可以通过“设置”活动上的两个开关按钮选择是否要查看它们。我想在用户单击通知时打开活动访问者。我写了下面的代码,但这没有发生,我应该写什么来代替

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);



                }

正如Android文档中所述,您需要设置当用户点击构建器上的通知时将触发的意图:

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