3/29/2560

วิธีสร้างการแจ้งเตือนใน Android โดย Notification และการทำงานซ้ำโดย AlarmManager




คุณสมบัติที่สำคัญอย่างหนึ่งสำหรับ Mobile Aplication คือ การแจ้งเตือน ( Notification ) เช่น แจ้งเตือนการนัดหมาย หรือแจ้งเตือนข่าวสาร ซึ่งหากเราต้องการให้ Application แจ้งเตือน ได้นั้นอาจต้องมีการทำงานซ้ำ เช่น ตรวจสอบว่ามีการ Update ข้อมูลใหม่ที่ Server หรือไม่ทุกๆ 1 นาที ซึ่งเราสามารถใช้ Class AlarmManager ของ Android ได้ และใช้ Class Notification เพื่อแจ้งเตือนได้ โดยมีตัวอย่างดังนี้

1. ในไฟล์  AndroidMainFest.xml  

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

<!-- Class Main -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<!-- หน้าที่จะแสดงเมื่อกดแจ้งเตือน -->
        <activity android:name=".get_activity">
        </activity>

<!-- ให้ สามารถใช้ Class AlarmReceiver สำหรับการแจ้งเตือนได้ -->
        <receiver
            android:name=".AlarmReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NOTIFY" />
            </intent-filter>
        </receiver>

    </application>

2. ในไฟล์ MainActivity.java 
สร้าง PendingIntent  เพื่อกำหนดว่าให้ AlarmReceiver เป็น Class ที่จะทำงานทุกๆ 1 นาที
PendingIntent pendingIntent = 
PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), 0);

กำหนดให้ Class AlarmReceiver ทำงานทุก 1 นาที
AlarmManager alarmManager = 
(AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 60*1000, pendingIntent);//ทำงานทุกๆ 6000 มิลิวินาที=1 นาที

3. สร้าง Class AlarmReceiver คือ Class ที่จะทำงานทุกๆ 1 นาที

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

//extends BroadcastReceiver ให้ สมารถทำงานเมื่อปิด App ได้

public class AlarmReceiver extends BroadcastReceiver

    @Override //method onReceive คือ method หลักที่จะทำงาน
    public void onReceive(Context context, Intent intent) {

//กำหนดให้ get_activity คือหน้าที่จะเปิดเมื่อกดที่การแจ้งเตือน
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, get_activity.class), 0);

        //สร้างการแจ้งเตือน
        Notification notification =
                new NotificationCompat.Builder(context) 
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("แจ้งเตือน")//หัวข้อที่จะแจ้งเตือน
                        .setContentText("www.projectsoft.biz")//ข้อความที่จะแจ้งเตือน
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .build();

       //แสดงการแจ้งเตือนด้านบน
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify(999, notification);

    }
}

เมื่อ รันโปรแกรมจะพบว่าจะมีการแจ้งเตือนทำงานทุกๆ 1 นาที