Unity Local Notification 구현하기 (로컬 푸시)
개발환경
- Compatible with Unity 2018.3 or above.
- Compatible with Android 4.4+ (API 19) and iOS 10.0+.
구현 하기
- Package Manager에서
Mobile Notifications
를 Import 합니다.

Project setting/Mobile Notification Setting
에서 아이콘 설정이 가능합니다.

- 구현 코드
- 테스트
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Notifications.Android; | |
using UnityEngine; | |
public class LocalNotificationDemo : MonoBehaviour | |
{ | |
private const string ChannelId = "channel_id"; | |
private void Start() | |
{ | |
CheckNotificationIntentData(); | |
RegisterNotificationChannel(); | |
AndroidNotificationCenter.OnNotificationReceived += OnNotificationRevied; | |
} | |
private void CheckNotificationIntentData() | |
{ | |
var notificationIntentData = AndroidNotificationCenter.GetLastNotificationIntent(); | |
if (notificationIntentData != null) | |
{ | |
var id = notificationIntentData.Id; | |
var channel = notificationIntentData.Channel; | |
var notification = notificationIntentData.Notification; | |
var msg = "Notification IntentData : id: " + id; | |
msg += "\n .channel: " + channel; | |
msg += "\n .notification: " + notification; | |
Debug.Log(msg); | |
} | |
} | |
private void OnNotificationRevied(AndroidNotificationIntentData data) | |
{ | |
var msg = "Notification received : " + data.Id + "\n"; | |
msg += "\n Notification received: "; | |
msg += "\n .Title: " + data.Notification.Title; | |
msg += "\n .Body: " + data.Notification.Text; | |
msg += "\n .Channel: " + data.Channel; | |
Debug.Log(msg); | |
} | |
private void RegisterNotificationChannel() | |
{ | |
var c = new AndroidNotificationChannel() | |
{ | |
Id = ChannelId, | |
Name = "Default Channel", | |
Importance = Importance.High, | |
Description = "Generic notifications", | |
}; | |
AndroidNotificationCenter.RegisterNotificationChannel(c); | |
} | |
public void OnClickedNotification() | |
{ | |
var notification = new AndroidNotification(); | |
notification.Title = "Test Title"; | |
notification.Text = "Test Text"; | |
notification.FireTime = System.DateTime.Now.AddMinutes(1); | |
notification.SmallIcon = "app_icon_id"; | |
notification.LargeIcon = "app_large_icon_id"; | |
notification.IntentData = "{\"title\": \"Notification 1\", \"data\": \"200\"}"; | |
AndroidNotificationCenter.SendNotification(notification, ChannelId); | |
} | |
} |
참고
https://docs.unity3d.com/Packages/com.unity.mobile.notifications@1.3/manual/index.html
반응형
'Unity' 카테고리의 다른 글
Input System #1 (0) | 2021.09.30 |
---|---|
Addressable Asset System 예제 (2) | 2021.05.25 |
Spine 연동 하기 (0) | 2021.05.09 |
Unity Ads 광고 구현하기 (1) | 2021.04.29 |
AdMob 광고 연동하기 (0) | 2021.04.27 |