Unity3d 在 iOS 上推送( push notification ) 編寫,unity3dios

來源:互聯網
上載者:User

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,將結果發送給伺服器。 我覺得可以使用一個單例,把整個這一套東西抽象出來,回頭有功夫整理一下。

如果你覺得這篇文章對你有協助,可以順手點個頂,不但不會喜當爹,還能讓更多人能看到它... 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.