How to issue a notification 간단한 알림 표시 하기
Android 2019. 11. 26. 23:07 |A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | public class MainActivity extends AppCompatActivity { NotificationManager notificationManager; private static String CHANNEL1_ID = "Channel1_ID"; private static String CHANNEL1_NAME = "Channel1_NAME"; private static String CHANNEL2_ID = "Channel2_ID"; private static String CHANNEL2_NAME = "Channel2_NAME"; private final int RequestCode = 101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ShowNotification1(); } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ShowNotification2(); } }); } public void ShowNotification1() { notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (notificationManager.getNotificationChannel(CHANNEL1_ID) == null) { notificationManager.createNotificationChannel(new NotificationChannel(CHANNEL1_ID, CHANNEL1_NAME, NotificationManager.IMPORTANCE_DEFAULT)); builder = new NotificationCompat.Builder(this, CHANNEL1_ID); } else { builder = new NotificationCompat.Builder(this, CHANNEL1_ID); } } else { builder = new NotificationCompat.Builder(this); // This package is part of the Android support library which is no longer maintained. } builder.setContentTitle("Simple notification"); builder.setContentText("Message for simple notification"); builder.setSmallIcon(android.R.drawable.ic_menu_view); Notification notification = builder.build(); notificationManager.notify(1, notification); // Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet // been canceled, it will be replaced by the updated information. // - Parameters // id: An identifier for this notification unique within your application. // notification: A Notification object describing what to show the user. Must not be null. } public void ShowNotification2() { notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (notificationManager.getNotificationChannel(CHANNEL2_ID) == null) { notificationManager.createNotificationChannel(new NotificationChannel(CHANNEL2_ID, CHANNEL2_NAME, NotificationManager.IMPORTANCE_DEFAULT)); builder = new NotificationCompat.Builder(this, CHANNEL2_ID); } else { builder = new NotificationCompat.Builder(this, CHANNEL2_ID); } } else { builder = new NotificationCompat.Builder(this); } Uri uri = Uri.parse("https://s-engineer.tistory.com"); Intent intent = new Intent(ACTION_VIEW, uri); // ACTION_VIEW: Display the data to the user. This is the most common action performed on data -- it is the generic action you can use on a piece of // data to get the most reasonable thing to occur. For example, when used on a contacts entry it will view the entry; when used on a mailto: URI it // will bring up a compose window filled with the information supplied by the URI; when used with a tel: URI it will invoke the dialer. PendingIntent pendingIntent = PendingIntent.getActivity(this, RequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentTitle("Notification with PendingIntent"); builder.setContentText("Message for notification with PendingIntent"); builder.setSmallIcon(android.R.drawable.ic_menu_view); builder.setAutoCancel(true); // Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. builder.setContentIntent(pendingIntent); // Supply a PendingIntent to send when the notification is clicked. Notification notification = builder.build(); notificationManager.notify(2, notification); } } |
Run the app and click "SIMPLE NOTIFICATION" button.
A simple notification appears as an icon in the status bar. Users can swipe down on the status bar to open the notification drawer, where they can view more details and take actions with the notification.
Go back to the app and click "NOTIFICATION WITH PENDINGINTENT" button. Then click the notification again.
It will take you to a great blog.
'Android' 카테고리의 다른 글
Create a simple Paint app for Android 간단한 안드로이드 그림판 만들기 (0) | 2019.12.08 |
---|---|
Multiple buttons with one listener 한 개의 리스너로 버튼 여러개 처리하기 (0) | 2019.12.04 |
Add a vibration and a ringtone notification 진동, 링톤 알림 추가하기 (0) | 2019.11.24 |
WebView - Display web content as part of your activity layout 앱 안에서 인터넷 웹 페이지 디스플레이 하기 (0) | 2019.11.11 |
Play Youtube videos with YouTubePlayer 유튜브 플레이어로 유튜브 영상 재생하기 (0) | 2019.10.24 |