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


文章作者:松阳

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

原文链接:http://blog.csdn.net/fansongy/article/details/43954515

width="150" height="210" frameborder="0" scrolling="no" src="http://widget.weibo.com/relationship/bulkfollow.php?language=zh_cn&uids=2080045857&wide=1&color=FFFFFF,FFFFFF,0082CB,666666&showtitle=0&showinfo=1&sense=0&verified=1&count=1&refer=http%3A%2F%2Fwww.himigame.com%2Fandroid-game%2F1521.html&dpc=1" style="font-size: 14px; font-weight: bold; border-width: 0px; margin: 0px; padding: 0px; font-family: arial, helvetica, clean, sans-serif; line-height: 16px;">







配置证书

先按照这个生成一大堆证书 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,将结果发送给服务器。 我觉得可以使用一个单例,把整个这一套东西抽象出来,回头有功夫整理一下。

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

你可能感兴趣的:(iOS,Unity3d)