นักผจญโค้ด (CODE HUNTER)

Implement Android Notification Listener using NotificationListenerService(ดักจับและตรวจสอบ notification ด้วย NotificationListenerService)



     ในการณีที่เราอยากตรวจสอบ Notifiction ว่าเป็นของแอพฯ ไหน เราสามารถทำได้โดยการสืบทอดคลาส NotificationListenerService

     เริ่มต้นด้วยการสร้าง Class ขึ้นมาใหม่ชื่อว่า MyNotificationListener และทำการสืบทอด NotificationListenerService และทำการ Override method สองตัว ได้แก่

- onNotificationPosted เมธอดถูกเรียกเมื่อมี notification เข้ามา เราสามารถตรวจสอบค่าได้ที่นี่
- onNotificationRemoved เมธอดถูกเรียกเมื่อ notification ถูกเคลียร์


     ทำการเพิ่มค่าเหล่านี้ที่ไฟล์ Manifest.xml

     เราต้องทำการ allow permission ตัว Notification access โดยเข้าไปที่ Settings->Security-Notification access

 
      

หรือจะเช็คด้วยโคด


อ่านเพิ่มเติมได้ที่  NotificationListenerService
Share:

Recent Posts

Popular Posts

About Me

About Me