我正在开发一个向用户显示通知和通知声音的应用。用户可以通过设置活动上的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("My Events")
.setContentText("Νέα εκδήλωση κοντά σας!")
.setChannelId(CHANNEL_ID).build();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
// Issue the notification.
mNotificationManager.notify(notifyID , notification);
}
似乎您只是忘记设置通知的点击操作:
Notification notification = new NotificationCompat.Builder(Settings.this)
// this line was missing
.setContentIntent(pIntent)
// continue with other attributes like before:
.setSmallIcon(R.drawable.notification)
.setContentTitle("My Events")
.setContentText("Νέα εκδήλωση κοντά σας!")
.setChannelId(CHANNEL_ID).build();
如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