728x90
반응형
MainAcitivity
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.yujin.background.service.MyService;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private static final int Interval = 1;
Button ntbNotl;
NotificationCompat.Builder mBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ntbNotl = findViewById(R.id.ntbNotl); //이벤트 연결
final Bitmap mLamg = BitmapFactory.decodeResource(getResources(), R.drawable.ba); //LargeIcon 라지아이콘 셋팅
PendingIntent plntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(MainActivity.this, "chnotl")
.setOngoing(true) //노티피케이션유지
.setSmallIcon(R.drawable.ba)
.setContentTitle("주문알림")
.setContentText("주문이 들어왔어요")
.setDefaults(Notification.DEFAULT_SOUND)
.setLargeIcon(mLamg)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setContentIntent(plntent);
ntbNotl.setOnClickListener(new View.OnClickListener() { //버튼 누르면 이벤트 시작
@Override
public void onClick(View view) {
NotificationManager notiMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26)
{
notiMan.createNotificationChannel(new NotificationChannel("chnotl", "채널", NotificationManager.IMPORTANCE_DEFAULT));
}
notiMan.notify(1004, mBuilder.build());
}
});
TimerTask addTask = new TimerTask() { //1분마다 알림생성
@Override
public void run() {
//주기적으로 실행할 작업 추가
NotificationManager notiMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26)
{
notiMan.createNotificationChannel(new NotificationChannel("chnotl", "채널", NotificationManager.IMPORTANCE_DEFAULT));
}
notiMan.notify(1004, mBuilder.build());
}
};
Timer timer = new Timer();
timer.schedule(addTask, 60, Interval * (60 * 10000)); //// 60초후 첫실행, Interval분마다 계속실행
}
}
activity_main
<LinearLayout 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">
<Button
android:id="@+id/ntbNotl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="알림 생성"
/>
</LinearLayout>
--결과--
궁금하신 사항은 댓글 남겨주세요.
728x90
반응형
'Android' 카테고리의 다른 글
App(앱) Icon(아이콘) 수정하는 방법 (0) | 2019.09.24 |
---|---|
WebView 기본적인 사용 (0) | 2019.09.24 |
SharedPreferences (간단한 정보저장) (0) | 2019.09.10 |
비콘감지 (11) | 2019.09.10 |
EditText/TextView/ImageView/Button/Radio/RadioGroup/URI/Intent (0) | 2019.09.10 |
댓글