Mobile notificationsプッシュ通知で受信してタップした際、飛ぶSceneを変更する機能をアプリにつけたいのですが
分からない点があるので教えて下さい。
プッシュ通知をタップした場合には
AndroidNotificationCenter.GetLastNotificationIntent()でIDを受信して
そのIDで飛ぶ先を振り分けます。
問題はプッシュ通知ではなく通常のアプリを開いた時でその場合
AndroidNotificationCenter.GetLastNotificationIntent()で取得した
IDがnullになってしまい、コンパイルをするとエラーになってしまい
実機テストをすると画面が固まってしまいます。
エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Unity.Notifications.Android.AndroidNotificationCenter.GetLastNotificationIntent () (at Library/PackageCache/com.unity.mobile.notifications@1.0.0-preview.23/Runtime/Android/ AndroidNotificationCenter.cs:628) LocalNotification.Start () (at Assets/Scripts/LocalNotification.cs:49)
やってみたこと)
取得するIDを参照するstring型にしてみました。
※intのような値型にするとnullが入った場合、クラッシュするので。
しかしstring型にしたのにエラーがでます。
そこでnull許容型なら問題ないかと思い
var? notificationIntentData= AndroidNotificationCenter.GetLastNotificationIntent();
としたのですが別のエラーになります。
Assets/Scripts/LocalNotification.cs(49,39): error CS0029: Cannot implicitly convert type 'Unity.Notifications.Android.AndroidNotificationIntentData' to 'var?'
どうすればこの問題解決するでしょうか?
環境)
PC: mac
Unity2018.3.14f1
言語:C#
使用API:Mobile notifications 1.0.0
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using UnityEngine.SceneManagement; #if UNITY_ANDROID using Unity.Notifications.Android; #endif public class LocalNotification : MonoBehaviour { #if UNITY_ANDROID int setTime; int nowTime; int seconds; int activeHour; int activeMin; int activeFlag; int activeSeconds; int cateId; int randomNum; string title; string message; string prefskeyHour; string prefskeyMin; string prefskeyActive; string m_channelId = "eraname"; string intentLink; // Start is called before the first frame update void Start() { //通知用のチャンネルを作成する var c = new AndroidNotificationChannel{ Id = m_channelId, Name = "アプリ名", Importance = Importance.High, Description = "プッシュ通知", }; AndroidNotificationCenter.RegisterNotificationChannel(c); //notification受信処理 var notificationIntentData = AndroidNotificationCenter.GetLastNotificationIntent(); if(notificationIntentData != null) { var sceneName = notificationIntentData.Notification.IntentData; SceneManager.LoadScene(sceneName); }else{ SceneManager.LoadScene("Main"); } } //停止時にnotificationをセットする void OnApplicationPause(bool pauseStatus) { int bacDay = PlayerPrefs.GetInt("day", 40); DateTime now = DateTime.Now; int nowDay = now.Day; if(bacDay != nowDay) { if(pauseStatus == true) { randomNum = UnityEngine.Random.Range(0, 6); switch(randomNum) { case 0: message = "年齢を調べる"; intentLink = "Main"; break; case 1: message = "西暦を調べる"; intentLink = "Christian"; break; case 2: message = "年を調べる"; intentLink = "School"; break; case 3: message = "記念日を調べる"; intentLink = "Marriage"; break; case 4: message = "祝いの年を調べる"; intentLink = "Longevity"; break; case 5: message = "名称と年を調べる"; intentLink = "Court"; break; } uniNotification("テスト対応",message, 86400, intentLink); PlayerPrefs.SetInt("day", nowDay); PlayerPrefs.Save(); }else{ return; } }else{ return; } } public void uniNotification(string title, string message, int seconds, string intentLink){ var notification = new AndroidNotification(); notification.Title = title; notification.Text = message; notification.SmallIcon = "icon_0"; notification.LargeIcon = "icon_1"; //intentdata notification.IntentData = intentLink; notification.FireTime = DateTime.Now.AddSeconds(seconds); //固有の識別子を割り当てる var identifier = AndroidNotificationCenter.SendNotification(notification, m_channelId); if(AndroidNotificationCenter.CheckScheduledNotificationStatus(identifier) == NotificationStatus.Scheduled) { AndroidNotificationCenter.UpdateScheduledNotification(identifier, notification, m_channelId); }else if(AndroidNotificationCenter.CheckScheduledNotificationStatus(identifier) == NotificationStatus.Delivered) { AndroidNotificationCenter.CancelNotification(identifier); }else if(AndroidNotificationCenter.CheckScheduledNotificationStatus(identifier)== NotificationStatus.Unknown) { AndroidNotificationCenter.SendNotification(notification, m_channelId); } } #endif }
回答1件
あなたの回答
tips
プレビュー