【ハルシオンブログ】Unityのパッケージマネージャーにローカル通知のやつがあったので試してみたよ(Android)
こんにちは。坂内っす。
そういえばUnity+スマホでのローカル通知って、今簡単にできるんです?
試しにやってみました。
あ、iOS版は色々めんどそうなので、Android版のみ行っています。
①パッケージマネージャーで、「Mobile Notifications」をインストール

②Project SettingにMobileNotificationの項目が増えているので、アイコンタイプとアイコンを指定

③ローカル通知用クラスの作成
【LocalNotification.cs】
で、呼ぶところはこんな感じ。
【Blog2020629.cs】
こんな感じ。
これで起動すると、ちゃんとローカル通知が来るよ!

あっ!!

アイコン(小)がでてない!?
で、確認すると・・・・

小さい方のプレビューが出てませんね。
もとのアイコンのファイルが1024x1024で大きいからだめなのかな?
こんなのが書いてますね。
小さいアイコンは48x48で、透過背景に白だけで書いてね。
なるほど。
そんな感じで小さいアイコンできるらしいっす。

48x48でこんなの作ってみた。

なったw
こんな感じで、簡単にローカル通知が導入できるようになりましたね!
問題はIOSだけど・・・・・それはまた・・・・今度‥書くかもしれないし、書かないかもしれない!
では、あでゅ~ノシ
そういえばUnity+スマホでのローカル通知って、今簡単にできるんです?
試しにやってみました。
あ、iOS版は色々めんどそうなので、Android版のみ行っています。
①パッケージマネージャーで、「Mobile Notifications」をインストール

②Project SettingにMobileNotificationの項目が増えているので、アイコンタイプとアイコンを指定

③ローカル通知用クラスの作成
【LocalNotification.cs】
using Unity.Notifications.Android;
public static class LocalNotification {
private static string ChannelId = "ch_0";
///
/// 通知チャンネルの登録
///
public static void Initialize() {
AndroidNotificationChannel channel = new AndroidNotificationChannel{
Id = ChannelId,
Name = "Channel Name",
Importance = Importance.High,
Description = "Description"
};
AndroidNotificationCenter.RegisterNotificationChannel(channel);
}
///
/// ローカル通知を登録
///
/// タイトル
/// 本文
/// 何秒後に通知をするか
public static void ReserveNotification(string title, string body, int afterSec) {
// 通知チャンネルを登録
Initialize();
// 通知を送信する
AndroidNotification notification = new AndroidNotification {
Title = title,
Text = body,
// アイコンをそれぞれセット
SmallIcon = "icon_0",
LargeIcon = "icon_1",
// 今から何秒後に通知をするか?
FireTime = System.DateTime.Now.AddSeconds(afterSec)
};
AndroidNotificationCenter.SendNotification(notification, ChannelId);
}
}
で、呼ぶところはこんな感じ。
【Blog2020629.cs】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blog20200629 : MonoBehaviour
{
void Start()
{
LocalNotification.ReserveNotification("ポケガからのお知らせ", "冒険がおわったよ。きてみてさわって。", 30);
}
}
こんな感じ。
これで起動すると、ちゃんとローカル通知が来るよ!

あっ!!

アイコン(小)がでてない!?
で、確認すると・・・・

小さい方のプレビューが出てませんね。
もとのアイコンのファイルが1024x1024で大きいからだめなのかな?
こんなのが書いてますね。
Only icons added to this list or manually added to the `res/drawable` folder can be used by notifications.
Small icons can only be composed simply of white pixels on a transparent backdrop and must be at least 48x48 pixels.
Large icons can contain any colors but must be not smaller than 192x192 pixels.
小さいアイコンは48x48で、透過背景に白だけで書いてね。
なるほど。
そんな感じで小さいアイコンできるらしいっす。

48x48でこんなの作ってみた。

なったw
こんな感じで、簡単にローカル通知が導入できるようになりましたね!
問題はIOSだけど・・・・・それはまた・・・・今度‥書くかもしれないし、書かないかもしれない!
では、あでゅ~ノシ
Category: 開発日記(Unity)
« 【ハルシオンブログ】Toggleで値を変えたときにonValueChangedで処理したくないとき | 【ハルシオンブログ】Visual Studio でオートフォーマットの無効化 »
コメント
| h o m e |