날짜, 시간을 String으로 지정한 후 정해진 시간에 푸시 알림을 받아보겠습니다.
1. Manifest에 다음과 같은 소스를 추가해줍니다.
AndroidManifest.xml
<receiver android:name=".AlarmRecevier"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
2. MainActivity.class
package com.example.alarmtest; //패키지명
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private GregorianCalendar mCalender;
private NotificationManager notificationManager;
NotificationCompat.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mCalender = new GregorianCalendar();
Log.v("HelloAlarmActivity", mCalender.getTime().toString());
setContentView(R.layout.activity_main);
//접수일 알람 버튼
Button button = (Button) findViewById(R.id.btnNoti);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
//AlarmReceiver에 값 전달
Intent receiverIntent = new Intent(MainActivity.this, AlarmRecevier.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, receiverIntent, 0);
String from = "2020-05-27 10:31:00"; //임의로 날짜와 시간을 지정
//날짜 포맷을 바꿔주는 소스코드
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date datetime = null;
try {
datetime = dateFormat.parse(from);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(datetime);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),pendingIntent);
}
}
3.
package com.example.alarmtest;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
public class AlarmRecevier extends BroadcastReceiver {
public AlarmRecevier(){ }
NotificationManager manager;
NotificationCompat.Builder builder;
//오레오 이상은 반드시 채널을 설정해줘야 Notification이 작동함
private static String CHANNEL_ID = "channel1";
private static String CHANNEL_NAME = "Channel1";
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
builder = null;
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
manager.createNotificationChannel(
new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
);
builder = new NotificationCompat.Builder(context, CHANNEL_ID);
} else {
builder = new NotificationCompat.Builder(context);
}
//알림창 클릭 시 activity 화면 부름
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,101,intent2, PendingIntent.FLAG_UPDATE_CURRENT);
//알림창 제목
builder.setContentTitle("알람");
//알림창 아이콘
builder.setSmallIcon(R.drawable.ic_launcher_background);
//알림창 터치시 자동 삭제
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
manager.notify(1,notification);
}
}
4. 실행화면
'Programming' 카테고리의 다른 글
[Android Studio] Fragment에서 MapView (0) | 2020.05.10 |
---|---|
[Java Script] 반복 횟수 지정 후 간단한 연산 반복 (0) | 2019.12.18 |