7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unityで特定の日時にローカル通知する

Posted at

はじめに

Unity Mobile Notifications Package
こちらのiOS/Androidでプッシュ通知を実装できるパッケージを使ってiOSのプッシュ通知を行います。
IMG_4691.jpg

インストール

『Package Manager』から『Mobile Notifications』をインストールします。
スクリーンショット 2020-01-11 23.31.25.png

#使い方
今回は2020年1月1日20時20分に通知を送る設定にしてます。
特定の日時に送る場合は『iOSNotificationCalendarTrigger』を参照します。

PushTest.cs
using System.Collections;
using System.Collections.Generic;
using Unity.Notifications.iOS;
using UnityEngine;
using System;

public class PushScript : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {

    }

    IEnumerator RequestAuthorization()
    {
        using (var req = new AuthorizationRequest(AuthorizationOption.Alert | AuthorizationOption.Badge, true))
        {
            while (!req.IsFinished)
            {
                yield return null;
            }

            string res = "\n RequestAuthorization: \n";
            res += "\n finished: " + req.IsFinished;
            res += "\n granted :  " + req.Granted;
            res += "\n error:  " + req.Error;
            res += "\n deviceToken:  " + req.DeviceToken;
            Debug.Log(res);
        }

    }
    
    // ボタンが押された際に呼び出される関数
    public void OnClickBtn()
    {
        var calendarTrigger = new iOSNotificationCalendarTrigger()
        {
            Year = 2020,
            Month = 1,
            Day = 1,
            Hour = 20,
            Minute = 20,
            Repeats = false
        };

        var notification = new iOSNotification()
        {
            // You can optionally specify a custom identifier which can later be 
            // used to cancel the notification, if you don't set one, a unique 
            // string will be generated automatically.
            Identifier = "_notification_01",
            Title = "Title",
            Body = "Scheduled at: 2020年1月1日20時20分",
            Subtitle = "This is a subtitle, something, something important...",
            ShowInForeground = true,
            ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
            CategoryIdentifier = "category_a",
            ThreadIdentifier = "thread1",
            Trigger = calendarTrigger,
        };

        iOSNotificationCenter.ScheduleNotification(notification);
    }
}
7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?