iT邦幫忙

2021 iThome 鐵人賽

DAY 4
0
Mobile Development

android studio 30天學習筆記系列 第 4

android studio 30天學習筆記 -day 4-Notification

Notification一般會在手機上方的通知欄顯示,舉例來說,就像平時常用的line別人傳送過來的訊息或是簡訊,在不開啟app的情況下,能從手機上方看見通知訊息的內容,在生活中可說是相當便利的功能。

今天會用Notification實作一個自我接收的通知,通知的訊息是用editText輸入顯示,按下button送出此通知內容

建立NotificationChannel

public void createNotificationChannel(){ //初始化NotificationManager
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //創建NotificationChannel(String id,CharSequence name,int importance)對象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ChannelID,ChannelName, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }

建立NotificationCompat.Builder

 public NotificationCompat.Builder getNotificationChannelBuilder(String msg){//創建通知相關設定
        PendingIntent pendingIntent=PendingIntent.getActivity(this,
                0,new Intent(this,MainActivity.class)
                ,PendingIntent.FLAG_CANCEL_CURRENT);
        return builder =new NotificationCompat.Builder(this,ChannelID)
                .setContentTitle("message")
                .setContentText(msg)
                .setSmallIcon(R.drawable.ic_launcher_foreground)//必填
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
    }

setContentTitle:設定通知標題
setContentText:設定通知內容
setSmallIcon:設定小圖標,一定要填
setContentIntent:設定使用者點擊通知時要觸發的 intent 行為
setAutoCancel:讓通知在使用者點擊通知時自動移除

NotificationManager
是android系統負責管理及發送的類別

manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

notify
發出通知

manager.notify(1, builder.build());

MainActivity完整程式碼

public class MainActivity2 extends AppCompatActivity {
    private EditText editText;
    public static final String ChannelID="myChannel";
    public static final String ChannelName ="eles";
    private NotificationManager manager;
    private NotificationCompat.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText=findViewById(R.id.edit1);
        createNotificationChannel();//創建頻道

    }
    //按鈕send監聽事件
    public void send(View view) { //送出通知
        sendNotificationMsg();
    }
    public void createNotificationChannel(){ //初始化NotificationManager
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //創建NotificationChannel(String id,CharSequence name,int importance)對象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ChannelID,ChannelName, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }
    
    private void sendNotificationMsg() {
        //判斷editText有沒有輸入值
        if (!editText.getText().toString().isEmpty()) {
            builder = getNotificationChannelBuilder(editText.getText().toString());//發出的通知內容是editText輸入的字
            manager.notify(1, builder.build());
        }
        else{
            Toast.makeText(this,"不能空白",Toast.LENGTH_LONG).show();
        }
    }

    public NotificationCompat.Builder getNotificationChannelBuilder(String msg){//創建通知相關設定
        PendingIntent pendingIntent=PendingIntent.getActivity(this,
                0,new Intent(this,MainActivity.class)
                ,PendingIntent.FLAG_CANCEL_CURRENT);
        return builder =new NotificationCompat.Builder(this,ChannelID)
                .setContentTitle("message")
                .setContentText(msg)
                .setSmallIcon(R.drawable.ic_launcher_foreground)//必填
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
    }
}

結果
https://ithelp.ithome.com.tw/upload/images/20210705/20138966z7p8js9C8R.jpg
https://ithelp.ithome.com.tw/upload/images/20210705/2013896690fp4gLdT8.jpg


上一篇
android studio 30天學習筆記-day 3 -介紹Service
下一篇
android studio 30天學習筆記-day 5-介紹retrofit(一)
系列文
android studio 30天學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言