Posted at

Unity5でローカル通知 (iOS)

More than 3 years have passed since last update.

Unity4では動いたのにUnity5になってからビルドでエラーをはいたので修正したメモ。


plug-in

まず一番つまずいたのがplug-inの最初の一行目

Unity4までは #import "iPhone_target_Prefix.pch" 

Unity5からは #import "Prefix.pch"

に変更された.


LocalNotificationController.mm

#import "Prefix.pch"

#import "UnityAppController.h"

@interface LocalNotificationController : UnityAppController
+(void)load;
@end

@implementation LocalNotificationController
+(void)load
{
extern const char* AppControllerClassName;
AppControllerClassName = "LocalNotificationController";
}

// アプリが起動時実行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{

[super application:application didFinishLaunchingWithOptions:launchOptions];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0)
{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
}
return YES;
}

@end



実装

Unity5よりネームスペース"UnityEngine.iOS"を記述する必要がでてきた.


LocalNotifyScript.cs

using UnityEngine;

using System.Collections;

public class LocalNotifyScript : MonoBehaviour {

void Start ()
{
addLocalPush ();
}

void addLocalPush ()
{

Debug.Log ("ローカル通知が走ります");
#if UNITY_EDITOR
#elif UNITY_ANDROID
#else
addLocalPushiOS();
#endif
}

void addLocalPushiOS()
{
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification();
l.applicationIconBadgeNumber = 1;
l.fireDate = System.DateTime.Now.AddSeconds(10);
l.alertBody = "通知テストだよ!";
UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(l);
}

}
@end


以上修正でビルド出来るようになった.


参考サイト

Unity ローカルプッシュ通知処理を書いてみた iOS編