这是我第一次使用Notification。
使用此代码,我可以阅读Log()中写的内容:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
Intent intentDown = new Intent(this, myIntentClass.class);
intentDown.putExtra("DO", "down");
PendingIntent pDown = PendingIntent.getActivity(this,0,intentDown,0);
contentView.setOnClickPendingIntent(R.id.drop_down, pDown);
Intent intentUp = new Intent(this, myIntentClass.class);
intentUp.putExtra("DO", "up");
PendingIntent pUp = PendingIntent.getActivity(this,1,intentUp,0);
contentView.setOnClickPendingIntent(R.id.drop_up, pUp);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channelSmoke")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContent(contentView)
.setSubText("Sub Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setOngoing(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(100, builder.build());
这是我的Intent课程:
public class myIntentClass extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String action = (String) Objects.requireNonNull(getIntent().getExtras()).get("DO");
assert action != null;
if (action.equals("down")) {
Log.d("CLICKED", "DOWN");
finish();
}else if(action.equals("up")){
Log.d("CLICKED"," UP");
finish();
}
}
}
但是,当我单击“ R.id.drop_down”或“ R.id.drop_up”按钮时,该应用会打开。可以打开未打开的应用程序(例如使用chromecast的netflix通知)。
编辑:我解决了使用接收器而不是IntentClass的问题。
只需使用Reciever而不是IntentClass