Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

สร้างและจัดการช่องการแจ้งเตือนใน Android


ตัวอย่างนี้สาธิตเกี่ยวกับการสร้างและจัดการช่องการแจ้งเตือนใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

<ก่อน> <ปุ่ม android :id ="@+id/btnCreateNotification" android :layout_width ="0dp" android :layout_height ="wrap_content" android :text ="สร้างการแจ้งเตือน" แอป :layout_constraintBottom_toBottomOf =แอป "พาเรนต์" :layout_constraintEnd_toEndOf =แอป "พาเรนต์" :layout_constraintStart_toStartOf =แอป "พาเรนต์" :layout_constraintTop_toTopOf ="พาเรนต์" />

ขั้นตอนที่ 3 − เพิ่มเสียงลงในโฟลเดอร์ดิบ

สร้างและจัดการช่องการแจ้งเตือนใน Android

ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

แพ็คเกจ app.tutorialspoint.com.notifyme;นำเข้า android.app.NotificationChannel;นำเข้า android.app.NotificationManager;นำเข้า android.content.ContentResolver;นำเข้า android.content.Context;นำเข้า android.graphics.Color;นำเข้า android media.AudioAttributes;นำเข้า android.net.Uri;นำเข้า android.support.v4.app.NotificationCompat;นำเข้า android.support.v7.app.AppCompatActivity;นำเข้า android.os.Bundle;นำเข้า android.view.View;นำเข้า android widget.Button คลาสสาธารณะ MainActivity ขยาย AppCompatActivity { สตริงสุดท้ายแบบคงที่สาธารณะ NOTIFICATION_CHANNEL_ID ="10001"; สตริงคงที่ส่วนตัวสุดท้าย default_notification_channel_id ="default"; @Override ป้องกันโมฆะ onCreate (Bundle saveInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout. activity_main ); ปุ่ม btnCreateNotification =findViewById(R.id. btnCreateNotification ); btnCreateNotification.setOnClickListener ( View.OnClickListener ใหม่ () { @แทนที่โมฆะสาธารณะ onClick (ดู v) { เสียง Uri =Uri. parse (ContentResolver. SCHEME_ANDROID_RESOURCE + "://" + getPackageName () + "/raw/quite_impressed.mp3" ); NotificationCompat.Builder mBuilder =ใหม่ NotificationCompat.Builder(MainActivity. this, default_notification_channel_id ) .setSmallIcon(R.drawable. ic_launcher_foreground ) .setContentTitle( "Test" ) .setSound(Hellosound) .setContent! ThisText is my การแจ้งเตือน" ); NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE ); if (android.os.Build.VERSION. SDK_INT>=android.os.Build.VERSION_CODES. O ) { AudioAttributes audioAttributes =ใหม่ AudioAttributes.Builder () .setContentType(AudioAttributes. CONTENT_TYPE_SONIFICATION ) .setUsage(AudioAttributes USAGE_ALARM ) .build(); ความสำคัญจริง =NotificationManager IMPORTANCE_HIGH; NotificationChannel alertChannel =new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , ความสำคัญ); alertChannel.enableLights (จริง); alertChannel.setLightColor(สี แดง ); alertChannel.enableVibration(จริง); alertChannel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }); alertChannel.setSound(เสียง , audioAttributes); mBuilder.setChannelId ( NOTIFICATION_CHANNEL_ID ); ยืนยัน mNotificationManager !=null; mNotificationManager.createNotificationChannel (ช่องแจ้งเตือน); } ยืนยัน mNotificationManager !=null; mNotificationManager.notify(( int ) ระบบ currentTimeMillis (), mBuilder.build()); } }); }}

ขั้นตอนที่ 5 − เพิ่มรหัสต่อไปนี้ใน androidManifest.xml

<ก่อน><แสดง xmlns:android ="https://schema.android.com/apk/res/android" package ="app.tutorialspoint.com.notifyme"> <หมวดหมู่ android :name ="android.intent .category.LAUNCHER" />

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

สร้างและจัดการช่องการแจ้งเตือนใน Android