FC2ブログ
    04 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.» 06

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【ハルシオンブログ】Unityのパッケージマネージャーにローカル通知のやつがあったので試してみたよ(Android) 

    こんにちは。坂内っす。

    そういえば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)

    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/713-bf842eaf
    この記事にトラックバックする(FC2ブログユーザー)