การแจ้งเตือน
มีปุ่ม 3 ปุ่ม
- ปุ่มที่ 1 Notify เป็นการแจ้งเตือน
- ปุ่มที่ 2 Update เป็นการอัพเดทข้อความแจ้งเตือน
- ปุ่มที่ 3 Cancel เป็นการยกเลิกการแจ้งเตือน
เมื่อผู้ใช้กดที่การแจ้งเตือน จะมีการเรียกหน้า Activity
ที่เราเตรียมไว้ให้แสดงขึ้นมา (หน้า NotificationActivity)
แก้ไขหน้า activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/notify_button" android:layout_width="match_parent" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:text="Notify" /> <Button android:id="@+id/update_button" android:layout_width="match_parent" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:text="Update" /> <Button android:id="@+id/cancel_button" android:layout_width="match_parent" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:text="Cancel" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
แก้ไขหน้า MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.phaisarn.jack.testnotifyapplication; import android.app.NotificationManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Notification; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v4.app.NotificationCompat; import android.view.View; public class MainActivity extends AppCompatActivity { private final int NOTIFY_ID = 0; private NotificationCompat.Builder mNotifyBuilder; private NotificationManager mNotifyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); (findViewById(R.id.notify_button)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent( MainActivity.this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.myicon); mNotifyBuilder = new NotificationCompat.Builder(MainActivity.this) .setTicker("New Alert") .setSmallIcon(R.drawable.myicon) .setLargeIcon(largeIcon) .setContentTitle("You have got new alert") .setContentText("You have got a good news.") .setContentIntent(pendingIntent) .setAutoCancel(true); mNotifyBuilder.build(); mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotifyManager.notify(NOTIFY_ID, mNotifyBuilder.build()); } }); (findViewById(R.id.update_button)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mNotifyBuilder.setContentText("This is an update text."); Notification notification = mNotifyBuilder.build(); mNotifyManager.notify(NOTIFY_ID, notification); } }); (findViewById(R.id.cancel_button)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mNotifyManager.cancel(NOTIFY_ID); } }); } } |
บรรทัดที่ 25: สร้าง Notification
บรรทัดที่ 53: อัพเดท Notification
บรรทัดที่ 64: ยกเลิก Notification
สร้าง Activity ขึ้นมาใหม่แบบ Empty ชื่อ NotificationActivity
แก้ไขหน้า activity_notification.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NotificationActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is your notification" /> </android.support.constraint.ConstraintLayout> |
แก้ไขหน้า NotificationActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.phaisarn.jack.testnotifyapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class NotificationActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); } } |
เพิ่มไฟล์รูป myicon.png ไว้ใน
ProjectName\app\src\main\res\drawable\
เมื่อรันแล้วจะได้หน้าตาแบบนี้
เมื่อกดปุ่ม Notify
เมื่อกดปุ่ม Update
เมื่อกดปุ่ม Cancel
การแจ้งเตือนก็จะหายไป
ถ้ากดที่แถบ Notify
จะแสดงหน้าตาแบบนี้