はじめに
Unity Mobile Notifications Package
こちらのiOS/Androidでプッシュ通知を実装できるパッケージを使ってiOSのプッシュ通知を行います。
インストール
『Package Manager』から『Mobile Notifications』をインストールします。
#使い方
今回は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);
}
}