質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2725閲覧

Unity)nullが入る時の処理

navesanta

総合スコア198

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2019/07/28 04:43

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 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

k.matsuda

2019/07/30 00:44

unityのことはよくわかりませんが 最初のエラーは、 「オブジェクト参照がオブジェクトのインスタンスに設定されていません。」 というエラーです。
navesanta

2019/07/30 04:46

>「オブジェクト参照がオブジェクトのインスタンスに設定されていません。」 というエラーです。 回答ありがとうございます。 下記コードで var notificationIntentData = AndroidNotificationCenter.GetLastNotificationIntent(); notificationに設置したIntentを取得するのですが(今回の場合、intentLink) notificationをタップした以外の場合、Intentが取得できないのが 原因だと思っています。(nullになってしまう) var notificationIntentDataをnull許容型にすれば解決すると 思ったのですがvar?という形にするとエラーになりどうすれば良いのか悩んでいます。 var自体が型推論のためいけないのかもしれません。
k.matsuda

2019/07/30 04:56

型名は AndroidNotificationIntentDataのようです。 null許容できるかどうかは分かりませんが・・・。
k.matsuda

2019/07/30 05:09

プッシュ通知以外から起動した場合は、nullを返すとあるし、大丈夫かな?
guest

回答1

0

ベストアンサー

エラー文を見るにそもそもAndroidNotificationCenter.GetLastNotificationIntentの内部でエラーが起こってます。(AndroidNotificationCenter.cs:628って書いてある通り)

AndroidNotificationCenter.GetLastNotificationIntent() throws null exception. : Unity3D
こちらにある通り、上記のメソッド内で適宜nullチェックを行うよう修正して、プッシュ通知以外の場合はAndroidNotificationCenter.GetLastNotificationIntentがnullを返すようにしてあげればいいんじゃないかと思います。

投稿2019/07/30 14:40

sakura_hana

総合スコア11427

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

navesanta

2019/07/31 08:01

ありがとうございました。問題解決しました。 エラーが起きなくなりました。 これからもよろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問