Unity3d 在 iOS 上推送( push notification ) 編寫,unity3dios
文章作者:松陽
本文出自 阿修羅道,禁止用於商業用途,轉載請註明出處。
原文連結:http://blog.csdn.net/fansongy/article/details/43954515
配置認證
先按照這個產生一大堆認證 http://www.cnblogs.com/gpwzw/archive/2012/03/31/apple_push_notification_services_tutorial_part_1-2.html
其中輸入的命令有些錯誤,可以參考 http://www.tuicool.com/articles/vy2MbmZ
openssl x509 -in aps_development.cer -inform der -out PushChatCert.pemopenssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12cat PushChatCert.pem PushChatKey.pem > ck.pem
配置好認證後,在UnityAppController.mm中的 didFinishLaunchingWithOptions函數添加:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { // use registerUserNotificationSettings [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } else { // use registerForRemoteNotifications [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; } #else // use registerForRemoteNotifications [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; #endif
這樣就完成了對註冊的配置,對於Unity3d內建的註冊,應該是不相容IOS8系統
function Start() {NotificationServices.RegisterForRemoteNotificationTypes(RemoteNotificationType.Alert | RemoteNotificationType.Badge | RemoteNotificationType.Sound);}function Update () {if (!tokenSent) {var token : byte[] = NotificationServices.deviceToken;if (token != null) {// send token to a providervar hexToken : String = "%" + System.BitConverter.ToString(token).Replace('-', '%');new WWW("http://"+address+"/?token="+hexToken);tokenSent = true;}}}
如果使用Unity3d可以在didRegisterForRemoteNotificationsWithDeviceToken函數中加入
NSString *tokenStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];UnitySendMessage("Bridge", "onPushID",tokenStr.UTF8String);另外就是使用update來處理token也是不可行的。
擷取token成功後,更改didRegisterForRemoteNotificationsWithDeviceToken函數,添加一個訊息發送回unity3D,將結果發送給伺服器。 我覺得可以使用一個單例,把整個這一套東西抽象出來,回頭有功夫整理一下。
如果你覺得這篇文章對你有協助,可以順手點個頂,不但不會喜當爹,還能讓更多人能看到它...