設定した時間になったら通知する機能をアプリに実装したく
プッシュ通知が装備できるUnity Mobile Notifications Packageと言うのが
あったので早速実装してみました。
Unity Mobile Notifications Package
そのあと、scriptableとPlayerPrefsで設定したデータを呼び出して
参照して問題なく通知が出ました。(下記のコードを書きました)
デバイスが休止状態になった時点から設定時間を計算してその時に通知するようにしました。
質問は
サーバーを使わずアプリ内でローカルプッシュ通知を実装しているため
デバイスのホームボタンで休止状態にした時は問題なく通知するのですが
右下のボタンで残っているアプリを表示して完全にアプリ終了した時は
通知が発生しません。
完全にアプリを終了した時に通知する方法はサーバーを立ててそちらから
送る形にしないとできないのでしょうか?
個人レベルでやっているのでできたらサーバーを立てるのではなく
それに近い方法で実装できたらありがたいのですがそういった裏技(?)は
ないのでしょうか?
環境)
PC: mac
Unity2018.3
言語:C#
LocalNotification.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using Unity.Notifications.Android; public class LocalNotification : MonoBehaviour { [SerializeField]Entity_catelist ecatelist; [SerializeField]Entity_poplist epoplist; int setTime; int nowTime; int seconds; int activeHour; int activeMin; int activeFlag; int activeSeconds; int cateId; string title; string message; string prefskeyHour; string prefskeyMin; string prefskeyActive; string m_channelId = "hoge"; // Start is called before the first frame update void Start() { //通知用のチャンネルを作成する var c = new AndroidNotificationChannel{ Id = m_channelId, Name = "hogeApp", Importance = Importance.High, Description = "ここに説明文", }; AndroidNotificationCenter.RegisterNotificationChannel(c); } // Update is called once per frame void Update() { } void OnApplicationPause(bool pauseStatus) { if(pauseStatus){ for(int i = 0; i < 13; i++){ prefskeyHour = "hour" + i; prefskeyMin = "min" + i; prefskeyActive = "active" + i; activeHour = PlayerPrefs.GetInt(prefskeyHour); activeMin = PlayerPrefs.GetInt(prefskeyMin); activeFlag = PlayerPrefs.GetInt(prefskeyActive, 2); cateId = epoplist.sheets[0].list[i].cate_id; title = ecatelist.sheets[0].list[cateId -1].cate_Japan; message = epoplist.sheets[0].list[i].popup; if(activeFlag == 1){ activeSeconds = TimeChangedScript(activeHour,activeMin); UnityNotification(title, message, activeSeconds); }else{ return; } } } } public void UnityNotification(string title, string message, int seconds){ var n = new AndroidNotification{ Title = title, Text = message, SmallIcon = "icon_0", LargeIcon = "icon_1", FireTime = DateTime.Now.AddSeconds(seconds), }; AndroidNotificationCenter.SendNotification(n, m_channelId); } //設定時間と現在の時間の差分を返す int TimeChangedScript(int hour, int min) { DateTime now = DateTime.Now; int nowHour = now.Hour; int nowMinute = now.Minute; setTime = hour * 60 + min; nowTime = nowHour * 60 + nowMinute; if(setTime > nowTime){ seconds = (setTime - nowTime) * 60; }else{ seconds = (1440 + setTime - nowTime) * 60; //1440 = 24 * 60:時間を分にして加算 } return seconds; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/15 04:30