原地址:http://www.iappfan.com/%E3%80%90unity3d%E3%80%91ios-%E6%8E%A8%E9%80%81%E5%AE%9E%E7%8E%B0/

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface UIApplication(SupressWarnings)
- (void)application:(UIApplication *)application app42didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken;
- (void)application:(UIApplication *)application app42didFailToRegisterForRemoteNotificationsWithError:(NSError *)err;
- (void)application:(UIApplication *)application app42didReceiveRemoteNotification:(NSDictionary *)userInfo; - (BOOL)application:(UIApplication *)application app42didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end
 
#import "App42PushHandlerInternal.h"
#import <objc/runtime.h> void registerForRemoteNotifications()
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
} char * listenerGameObject = ;
void setListenerGameObject(char * listenerName)
{
free(listenerGameObject);
listenerGameObject = ;
int len = strlen(listenerName);
listenerGameObject = malloc(len+);
strcpy(listenerGameObject, listenerName);
} @implementation UIApplication(App42PushHandlerInternal) +(void)load
{
NSLog(@"%s",__FUNCTION__);
method_exchangeImplementations(class_getInstanceMethod(self, @selector(setDelegate:)), class_getInstanceMethod(self, @selector(setApp42Delegate:))); UIApplication *app = [UIApplication sharedApplication];
NSLog(@"Initializing application: %@, %@", app, app.delegate);
} BOOL app42RunTimeDidFinishLaunching(id self, SEL _cmd, id application, id launchOptions)
{
BOOL result = YES; if ([self respondsToSelector:@selector(application:app42didFinishLaunchingWithOptions:)])
{
result = (BOOL) [self application:application app42didFinishLaunchingWithOptions:launchOptions];
}
else
{
[self applicationDidFinishLaunching:application];
result = YES;
} [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return result;
} void app42RunTimeDidRegisterForRemoteNotificationsWithDeviceToken(id self, SEL _cmd, id application, id devToken)
{
if ([self respondsToSelector:@selector(application:app42didRegisterForRemoteNotificationsWithDeviceToken:)])
{
[self application:application app42didRegisterForRemoteNotificationsWithDeviceToken:devToken];
}
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[devToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"deviceToken=%@",deviceToken);
const char * str = [deviceToken UTF8String];
UnitySendMessage(listenerGameObject, "onDidRegisterForRemoteNotificationsWithDeviceToken", str); } void app42RunTimeDidFailToRegisterForRemoteNotificationsWithError(id self, SEL _cmd, id application, id error)
{
if ([self respondsToSelector:@selector(application:app42didFailToRegisterForRemoteNotificationsWithError:)])
{
[self application:application app42didFailToRegisterForRemoteNotificationsWithError:error];
}
NSString *errorString = [error description];
const char * str = [errorString UTF8String];
UnitySendMessage(listenerGameObject, "onDidFailToRegisterForRemoteNotificationsWithError", str);
NSLog(@"Error registering for push notifications. Error: %@", error);
} void app42RunTimeDidReceiveRemoteNotification(id self, SEL _cmd, id application, id userInfo)
{
if ([self respondsToSelector:@selector(application:app42didReceiveRemoteNotification:)])
{
[self application:application app42didReceiveRemoteNotification:userInfo];
} NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
NSString *jsonString = nil;
if (! jsonData)
{
NSLog(@"Got an error: %@", error);
}
else
{
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonString= %@",jsonString);
} if (jsonString)
{
const char * str = [jsonString UTF8String];
UnitySendMessage(listenerGameObject, "onPushNotificationsReceived", str);
}
else
{
UnitySendMessage(listenerGameObject, "onPushNotificationsReceived", nil);
}
} static void exchangeMethodImplementations(Class class, SEL oldMethod, SEL newMethod, IMP impl, const char * signature)
{
Method method = nil;
//Check whether method exists in the class
method = class_getInstanceMethod(class, oldMethod); if (method)
{
//if method exists add a new method
class_addMethod(class, newMethod, impl, signature);
//and then exchange with original method implementation
method_exchangeImplementations(class_getInstanceMethod(class, oldMethod), class_getInstanceMethod(class, newMethod));
}
else
{
//if method does not exist, simply add as orignal method
class_addMethod(class, oldMethod, impl, signature);
}
} - (void) setApp42Delegate:(id<UIApplicationDelegate>)delegate
{ static Class delegateClass = nil; if(delegateClass == [delegate class])
{
[self setApp42Delegate:delegate];
return;
} delegateClass = [delegate class]; exchangeMethodImplementations(delegateClass, @selector(application:didFinishLaunchingWithOptions:),
@selector(application:app42didFinishLaunchingWithOptions:), (IMP)app42RunTimeDidFinishLaunching, "v@:::");
exchangeMethodImplementations(delegateClass, @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:),
@selector(application:app42didRegisterForRemoteNotificationsWithDeviceToken:), (IMP)app42RunTimeDidRegisterForRemoteNotificationsWithDeviceToken, "v@:::"); exchangeMethodImplementations(delegateClass, @selector(application:didFailToRegisterForRemoteNotificationsWithError:),
@selector(application:app42didFailToRegisterForRemoteNotificationsWithError:), (IMP)app42RunTimeDidFailToRegisterForRemoteNotificationsWithError, "v@:::"); exchangeMethodImplementations(delegateClass, @selector(application:didReceiveRemoteNotification:),
@selector(application:app42didReceiveRemoteNotification:), (IMP)app42RunTimeDidReceiveRemoteNotification, "v@:::"); [self setApp42Delegate:delegate];
} @end
PushScript.cs

 
 
 
 
 
 

C#

 
using UnityEngine;
using System.Collections;
using com.shephertz.app42.paas.sdk.csharp;
using com.shephertz.app42.paas.sdk.csharp.pushNotification;
using System;
using System.Runtime.InteropServices; public class PushScript : MonoBehaviour
{
const string api_key = "3c1d8c1d23e1dde0d820b06e33e6260e3b9ac0438d522a4ac9d524fc12cb8559";//"App42_App_Key";
const string secret_key = "254964c8a7fcc95cee0362adc2e0e06e0a64ec53c7a9e5279c11b3c4303edf73";//"App42_Secret_Key"; [System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void registerForRemoteNotifications(); [System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void setListenerGameObject(string listenerName); // Use this for initialization
void Start ()
{
Debug.Log("Start called");
setListenerGameObject(this.gameObject.name);// sets the name of the game object as a listener to which this script is assigned.
} //Sent when the application successfully registered with Apple Push Notification Service (APNS).
void onDidRegisterForRemoteNotificationsWithDeviceToken(string deviceToken)
{
if (deviceToken != null && deviceToken.Length!=)
{
registerDeviceTokenToApp42PushNotificationService(deviceToken,"User Name");
}
SendPushToUser("Suman","Hello, Unity!!");
} //Sent when the application failed to be registered with Apple Push Notification Service (APNS).
void onDidFailToRegisterForRemoteNotificationsWithError(string error)
{
Debug.Log(error);
} //Sent when the application Receives a push notification
void onPushNotificationsReceived(string pushMessageString)
{
Console.WriteLine("onPushNotificationsReceived");
//dump you code here
Debug.Log(pushMessageString);
} //Registers a user with the given device token to APP42 push notification service
void registerDeviceTokenToApp42PushNotificationService(string devToken,string userName)
{
ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);
PushNotificationService pushService = serviceAPI.BuildPushNotificationService();
pushService.StoreDeviceToken(userName,devToken,"iOS");
} //Sends push to a given user
void SendPushToUser(string userName,string message)
{
ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);
PushNotificationService pushService = serviceAPI.BuildPushNotificationService();
pushService.SendPushMessageToUser(userName,message);
} }

直接使用 http://docs.unity3d.com/Documentation/ScriptReference/NotificationServices.html

【Unity3D】iOS 推送实现的更多相关文章

  1. iOS推送证书转pem文件

    iOS推送证书转 .pem文件. 推送证书转pem文件openssl x509 -in apns_miaobozhibo.cer -inform der -out apns_miaobozhibo.p ...

  2. IOS 推送-客户端处理推送消息

    IOS 推送-客户端处理推送消息 1.推送调用顺序 APN push的消息到达后,UIApplicationDelegate有两个方法和处理消息有关: 1)application:didReceive ...

  3. IOS 推送-配置与代码编写

    IOS 推送配置与代码编写 这里介绍IOS的推送,本文章已经在IOS6/7/8上都能运行OK,按照道理IOS9应该没问题. 大纲: 1.文章前提 2.推送介绍 3.推送文件账号设置 4.推送证书介绍 ...

  4. IOS 推送消息 php做推送服务端

    IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPh ...

  5. 友盟iOS推送配置(从真机调试到推送)

    下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...

  6. iOS 推送全解析

    本文旨在对 iOS 推送(以下简称 推送)进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacod ...

  7. “iOS 推送通知”详解:从创建到设置到运行

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  8. 转载:iOS 推送的服务端实现

    参考网址1: iOS消息推送机制的实现 http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html 参考网址2: iOS 推送的 ...

  9. C#调用IOS推送

    C#调用IOS推送 使用的是 PushSharp 开源库 源码代码如下 点我

  10. iOS推送 再备

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

随机推荐

  1. POJ 2407 Relatives

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13920   Accepted: 6965 Description Give ...

  2. 3_1 wp8应用生命周期与导航事件[wp8特色开发与编程技巧]

    3_1生命周期 大家好,我是徐文康,今天我们来讨论一下应用程序的生命周期,首先打开app.xaml.cs文件我们可以看到这里有几个已经为我们写好的事件.   Launching 应用程序启动 Acti ...

  3. ADO.NET笔记——读取二进制大对象(BLOB)

    相关知识: 在SQL Server中,一般情况下,每行数据的总长度不能超过8K字节.因此,下列数据类型的长度,也不能超过8K字节:binary,char(),nchar(),varchar(),nva ...

  4. ADO.NET笔记——调用存储过程

    相关知识: 在ADO.NET访问SQL Server时,鼓励使用存储过程取代常规的SQL语句. 存储过程有下列优点: 存储过程中的SQL语句将会经过预先的解析和编译,然后存放在数据库服务器上行.调用的 ...

  5. Mysql数据库常用的命令 数据备份 恢复 远程

    远程数据库 格式: mysql -h主机地址 -u用户名 -p用户密码数据库 mysql -h 42.51.150.68 -u yang -p discuz mysql设置密码 mysql>us ...

  6. 京东sdk商家上架接口调用问题总结(更新中...)

    前言: 最近在做商家发布产品,调用京东sdk,发现问题很多,而且还是在我同事的帮助下完成的,摸索中,菜鸟还请高手门多多提携才好,入正题 首先是引用jd的sdk啦,京东sdk中发布商品需要调用一个 36 ...

  7. WordPress 主题开发 - (三) 开发工具 待翻译

    Before we get started building any WordPress Theme, we’re going to need to get our development tools ...

  8. DOS命令之----Netstat+Task以及相关使用

    作为一个初步接触电脑的人,在学习Android的过程中,遇到各种问题,今天遇到了.这样一个错误提示: The connection to adb is down, and a severe error ...

  9. (二)使用log4net写入数据库自定义日志

    1.配置项目环境 1.1 本文只显示需要修改配置的操作,初次引入log4net环境的请参考上文. 1.2 安装mysql-connector-net.msi环境,下载地址.并手动生成数据库日志信息表. ...

  10. opencv学习笔记(04)——ROI

    ROI的用法:1.直接相加:2.掩码法 #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgpro ...