Unity3d 在 iOS 上推送( push notification ) 编写


文章作者:松阳

本文出自 阿修罗道,禁止用于商业用途,转载请注明出处。  

原文链接: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.pem
openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12
cat 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 provider
				var 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,将结果发送给服务器。 我觉得可以使用一个单例,把整个这一套东西抽象出来,回头有功夫整理一下。

如果你觉得这篇文章对你有帮助,可以顺手点个,不但不会喜当爹,还能让更多人能看到它... 技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。