簡體   English   中英

啟用和禁用安卓通知

[英]To enable and disable the android notification

我想創建一個具有開關(啟用和禁用)按鈕的通知應用程序。當我單擊啟用按鈕時,通知來了,當我單擊禁用按鈕時,通知無法到達應用程序或停止。 在此處輸入圖片說明

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

}

MyFirebaseMessagingService 類中的通用代碼

val notificationBuilder = NotificationCompat.Builder(this) .setWhen(System.currentTimeMillis()) .setShowWhen(true) .setSmallIcon(R.mipmap.ic_notification) .setContentIntent(pendingIntent) .setContentTitle(title) .setContentText(body) .setLargeIcon( BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)) .setDefaults(Notification.DEFAULT_SOUND or 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()
    )

當 Switch 關閉時刪除或注釋上面的代碼。

如果您的開關打開,則調用 .notify(id,builder.build()) 否則刪除此行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM