如何通过点击通知不打开应用程序?
how not to open the app by clicking on the 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());
这是我的意图class:
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" 按钮时,应用程序打开了。可以不打开应用程序(如 netflix 通知,使用 chromecast)。
编辑:
我使用 reciever 而不是 IntentClass 解决了问题。
只需使用 Receiver 而不是 Intent 类
这是我第一次使用通知。
使用这段代码,我可以读取 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());
这是我的意图class:
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" 按钮时,应用程序打开了。可以不打开应用程序(如 netflix 通知,使用 chromecast)。
编辑: 我使用 reciever 而不是 IntentClass 解决了问题。
只需使用 Receiver 而不是 Intent 类