コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】iOS / Android でローカル通知を実装できる「Unity3D Android notification plugin」紹介

はじめに

「Unity3D Android notification plugin」を Unity プロジェクトに導入することで
iOS / Android でローカル通知を実装できるようになります

使用例

5 秒後に通知

LocalNotification.SendNotification
(
    id      : 1,
    delayMs : 5000,
    title   : "Title",
    message : "Long message text",
    bgColor : new Color32( 255, 68, 68, 255 )
);

f:id:baba_s:20180909150550p:plain

5 秒後に大きなアイコン付きで通知

LocalNotification.SendNotification
(
    id      : 1,
    delayMs : 5000,
    title   : "Title",
    message : "Long message text with big icon",
    bgColor : new Color32( 255, 68, 68, 255 ),
    sound   : true,
    vibrate : true,
    lights  : true,
    bigIcon : "app_icon"
);

f:id:baba_s:20180909150556p:plain

5 秒後に通知アクション

var a1 = new LocalNotification.Action( "background", "In Background", this );
a1.Foreground = false;
var a2 = new LocalNotification.Action( "foreground", "In Foreground", this );
LocalNotification.SendNotification
(
    id          : 1,
    delayMs     : 5000,
    title       : "Title",
    message     : "Long message text with actions",
    bgColor     : new Color32( 255, 68, 68, 255 ),
    sound       : true,
    vibrate     : true,
    lights      : true,
    bigIcon     : null,
    soundName   : "boing",
    channel     : "default",
    actions     : new [] { a1, a2 }
);

public void OnAction( string identifier )
{
    Debug.Log( "Got action " + identifier );
}

f:id:baba_s:20180909150613p:plain

5 秒後にリピート通知

LocalNotification.SendRepeatingNotification
( 
    id          : 1, 
    delayMs     : 5000, 
    timeoutMs   : 60000, 
    title       : "Title", 
    message     : "Long message text", 
    bgColor     : new Color32( 255, 68, 68, 255 ) 
);

通知をキャンセル

LocalNotification.CancelNotification( 1 );

小さなアイコンを設定する方法

「Assets/Plugins/Android/res/*」フォルダ内の「notify_icon_small.png」を差し替える

大きなアイコンを設定する方法

「Assets/Plugins/Android/res/*」フォルダ内に「notify_icon_big.png」を追加して
「bigIcon」引数に「notify_icon_big」と設定する
アプリアイコンを使用する場合は「bigIcon」引数に「app_icon」と設定する

カスタムサウンドを使用する方法

Android では「Assets/Plugins/Android/res/raw」フォルダ内に .mp3 か .ogg を追加する
iOS では「Assets/StreamingAssets」フォルダ内に .wav か .caf、.aiff を追加する
そして「sound」引数に「true」を設定し、「soundName」引数に、
拡張子抜きのサウンドファイル名を設定する