본문 바로가기
개발/C#

유니티 C# 로컬 푸시 알림 Local Push Notification 간단 구현

by SPNK 2022. 8. 17.
반응형

에셋 다운로드

 

Simple Android Notifications Free | 기능 통합 | Unity Asset Store

Use the Simple Android Notifications Free from Hippo on your next project. Find this integration tool & more on the Unity Asset Store.

assetstore.unity.com

 

코드 작성

using UnityEngine;
using System.Collections.Generic;
using System;
using Assets.SimpleAndroidNotifications;

#if UNITY_IOS
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
using LocalNotification = UnityEngine.iOS.LocalNotification;
#endif

public class LocalNotificationManager : MonoBehaviour
{
    string title;
    string content1, content2, content3, content4;

    void Start()
    {
        DeleteNotification();

#if UNITY_IOS
        NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);
#endif

        AddLocalNotification();
    }

    void DeleteNotification() //알람 초기화
    {
#if UNITY_ANDROID
        NotificationManager.CancelAll();
#elif UNITY_IOS
        NotificationServices.ClearLocalNotifications();
        NotificationServices.CancelAllLocalNotifications();
#endif
    }

    void AddLocalNotification() //알람 추가
    {
    
		title = "Game Title Name";
        content1 = "TXT_CONTENT1";
        content2 = "TXT_CONTENT2";
        content3 = "TXT_CONTENT3";
        content4 = "TXT_CONTENT4";
      
        //오늘
        DateTime dtToday = DateTime.Today;

        //매일 0시에 알림
        DateTime notify1 = DateTime.Now;
        TimeSpan time1 = DateTime.Today.AddDays(1) - notify1;

        //어플 종료 후 15분 후에 알림
        DateTime notify2 = DateTime.Now.AddMinutes(1);
        TimeSpan time2 = notify2 - DateTime.Now;

        //어플 종료 후 알림(점심)
        DateTime notify3 = Convert.ToDateTime(dtToday.ToString("yyyy/MM/dd") + " " + "12:30:00 PM");
        TimeSpan time3 = notify3 - DateTime.Now;

        //어플 종료 후 알림(저녁)
        DateTime notify4 = Convert.ToDateTime(dtToday.ToString("yyyy/MM/dd") + " " + "7:30:00 PM");
        TimeSpan time4 = notify4 - DateTime.Now;

        //어플 종료 후 특정 요일에 등록 알림
        DateTime dtNow = dtToday.AddDays(Convert.ToInt32(DayOfWeek.Friday) - Convert.ToInt32(dtToday.DayOfWeek));
        DateTime notify5 = Convert.ToDateTime(dtNow.ToString("yyyy/MM/dd") + " " + "9:00:00 PM");
        TimeSpan time5 = notify5 - DateTime.Now;

#if UNITY_ANDROID
        NotificationManager.SendWithAppIcon(time1, title, content1, Color.gray, NotificationIcon.Bell);
        
        Debug.Log("Set Android Notification");        
#elif UNITY_IOS

        LocalNotification noti1 = new LocalNotification();
        noti1.alertTitle = title;
        noti1.alertBody = content1;
        noti1.soundName = LocalNotification.defaultSoundName;
        noti1.applicationIconBadgeNumber = 1;
        noti1.fireDate = notify1;
        NotificationServices.ScheduleLocalNotification(noti1);

        if (time2.Ticks > 0)
        {
           LocalNotification noti2 = new LocalNotification();
           noti2.alertTitle = title;
           noti2.alertBody = content2;
           noti2.soundName = LocalNotification.defaultSoundName;
           noti2.applicationIconBadgeNumber = 1;
           noti2.fireDate = notify2;
           NotificationServices.ScheduleLocalNotification(noti2);
        }

        if (time3.Ticks > 0)
        {
           LocalNotification noti3 = new LocalNotification();
           noti3.alertTitle = title;
           noti3.alertBody = content3;
           noti3.soundName = LocalNotification.defaultSoundName;
           noti3.applicationIconBadgeNumber = 1;
           noti3.fireDate = notify3;
           NotificationServices.ScheduleLocalNotification(noti3);
        }

        Debug.Log("Set IOS Notification");
#endif
    }
}

 


참고할만한 글

 

 

유니티 C# 비속어 필터 적용 inputfield 간단 사용법

Assets / Resoures / BadWord.txt 준비 코드 작성 using System.Collections; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public I

parksh3641.tistory.com

 

 

유니티 C# 자석 효과 Magnet 간단 구현

코드 작성 (적용하고 싶은 오브젝트에 적용) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public Transform target; public float moveSpeed = 1.0f; p

parksh3641.tistory.com

 

반응형

댓글