Android 《Notification》

一个小笨蛋發表於2024-03-11

程式碼

package com.xian.app.broadcast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private NotificationManager manager;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_send_notification).setOnClickListener(this);
        findViewById(R.id.btn_close_notification).setOnClickListener(this);

        initNotify();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
			case R.id.btn_send_notification:
                manager.notify(1,notification); //ID要一致
                break;
            case R.id.btn_close_notification: // ID 要一致
                manager.cancel(1);
                break;
        }
    }

    private void SendNotification() {
        manager.notify(1,notification);
    }

    /***
     * 傳送Notification
     */
    private static final String channelId = "xian";
    private void initNotify() {
        manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
        //相容性處理
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){
            /**
             *     public static final int IMPORTANCE_NONE = 0;
             *       關閉通知
             *     public static final int IMPORTANCE_MIN = 1;
             *       開啟通知,不會彈出,沒有提示音,狀態列中沒顯示
             *    public static final int IMPORTANCE_LOW = 2;
             *      開啟通知,不會彈出,沒有提示音,狀態列中有顯示
             *    public static final int IMPORTANCE_DEFAULT = 3;
             *       開啟通知,不會彈出,發出提示音,狀態列中有顯示
             *    public static final int IMPORTANCE_HIGH = 4;
             *        開啟通知,會彈出,發出提示音,狀態列中有顯示
             */
            NotificationChannel channel = new NotificationChannel(channelId, "測試通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);

        }
		Intent intent = new Intent(this,ReturnDeskActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        //FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT
        PendingIntent pendingIntent = PendingIntent.getActivity(this,10086,intent,0);
        notification = new NotificationCompat.Builder(this, channelId)
                .setContentTitle("你有一個新的通知")
                .setContentText("銀行卡新收入500w人民幣")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setColor(Color.parseColor("#ff0000"))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground)) //設定大圖示
                .setContentIntent(pendingIntent) //點選通知後跳轉PendingIntent
                .setAutoCancel(true) //點選通知後是否自動取消
                .build();
    }
}

效果圖

image

相關文章