프로그래밍/Android

알림 예제(Notification Example)

DevIt 2016. 12. 20. 00:13

알림 예제(Notification Example)

 버튼 누르면 toast 메시지가 뜨고 진동과 함께 상태창에 알림이 온다.

상태창의 알림을 터치하면 SubActivity가 호출된다.


activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.notificationexample.mystoryg.notificationexample.MainActivity">

<Button
android:id="@+id/notification"
android:text="notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>


MainActivity.java

 package com.notificationexample.mystoryg.notificationexample;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button noti = (Button) findViewById(R.id.notification);
noti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast();
showNotification();
}
});
}

private void showToast() {
Toast toast = Toast.makeText(this, "알림을 확인해주세요!", Toast.LENGTH_LONG);
toast.show();
}

private void showNotification() {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, SubActivity.class)

.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("알림").setContentText("터치해주세요.").setTicker("확인해주세요.")

.setSmallIcon(R.drawable.email).setContentIntent(pendingIntent).setAutoCancel(true)

.setWhen(System.currentTimeMillis()).setDefaults(Notification.DEFAULT_ALL);

nm.notify(1, builder.getNotification());
}
}


activity_sub.xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_sub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.notificationexample.mystoryg.notificationexample.SubActivity">

<TextView
android:text="확인하셨습니다."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/home"
android:text="처음으로"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


SubActivity.java

 package com.notificationexample.mystoryg.notificationexample;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SubActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);

Button home = (Button)findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
}
}