我想创建一个具有切换(启用和禁用)按钮的通知应用程序。当我单击“启用”按钮时,会出现通知;当我单击“禁用”按钮时,通知将无法到达应用程序或停止。{{3} }
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "channel_id";
Button sendBtn;
Switch switchBtn,switchbtn2;
SharedPreferences.Editor prefEditor;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn= findViewById(R.id.send_token);
prefEditor=
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
switchbtn2= findViewById(R.id.switch3);
switchbtn2.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b) {
Switch a = switchbtn2;
if(a.isClickable()) {
if (switchbtn2.isChecked()) {
shownotifi();
prefEditor.putString("checked", "yes");
prefEditor.apply();
}
}
else{
//To Stop notification?????
prefEditor.putString("checked","no");
prefEditor.apply();
}
}
public void shownotifi() {
NotificationCompat.Builder builder = new
NotificationCompat.Builder(this);
builder.setContentTitle("This is heading");
builder.setContentText("This is description");
builder.setSmallIcon(R.drawable.ic_launcher_background);
Notification notification = builder.build();
NotificationManager notificationManager= .
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
});
}
答案 0 :(得分:0)
MyFirebaseMessagingService类中的通用代码
val notificationBuilder = NotificationCompat.Builder(this) .setWhen(System.currentTimeMillis()) .setShowWhen(true) .setSmallIcon(R.mipmap.ic_notification) .setContentIntent(pendingIntent) .setContentTitle(标题) .setContentText(body) .setLargeIcon(BitmapFactory.decodeResource(资源,R.mipmap.ic_launcher)) .setDefaults(Notification.DEFAULT_SOUND或Notification.DEFAULT_VIBRATE) .setColor(ContextCompat.getColor(applicationContext,R.color.colorPrimary)) .setAutoCancel(true) .setGroupSummary(true) val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val NOTIFICATION_CHANNEL_ID = resources.getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel =
NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, importance)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.BLUE
notificationChannel.enableVibration(true)
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
notificationManager.createNotificationChannel(notificationChannel)
}
当开关打开时
notificationManager.notify(
(System.currentTimeMillis() and 0xfffffff).toInt(),
notificationBuilder.build()
)
关闭“开关”后,请删除或注释上面的代码。
答案 1 :(得分:-1)
如果您的开关处于打开状态,请致电.notify(id,builder.build())否则删除此行。