【Unity3D】iOS 推送实现
ios 实现 Unity3D 推送
2023-09-14 08:57:08 时间
原地址: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 = 0; void setListenerGameObject(char * listenerName) { free(listenerGameObject); listenerGameObject = 0; int len = strlen(listenerName); listenerGameObject = malloc(len+1); 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
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!=0) { 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
相关文章
- (NO.00005)iOS实现炸弹人游戏(十):游戏主角(三)
- (NO.00001)iOS游戏SpeedBoy Lite成形记(一)
- Unity3D研究院之IOS本地消息通知LocalNotification的使用
- iOS APP 上传
- iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)
- iOS开发之使用XMPPFramework实现即时通信(二)
- iOS系统声音列表
- iOS设计模式 - 原型
- iOS短信验证码SDK集成文档
- IOS开发之实现App消息推送(最新)(1)
- iOS学习之VFL语言简介
- 【IOS-COCOS2D游戏开发之十五】详解CCPROGRESSTIMER 进度条并修改COCOS2D源码实现“理想”游戏进度条!
- 【IOS-COCOS2D游戏开发之二十二 】CCSPEED实现CCANIMATE动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- iOS开发那些事-故事板实现标签导航
- iOS开发那些事-平铺导航-基于Page的导航及案例实现
- EasyRTMP实现的一套简单、高效、易用的全平台(Windows/Linux/ARM/Android/iOS)RTMP直播推送库
- 44、自定义仿IOS对话框
- [Link]用HTML5/CSS3/JS开发Android/IOS应用
- ios swift 获取手机位置 定位
- iOS 绘制原理&异步绘制 离屏渲染 总结
- iOS swift Generics 通用类型
- iOS:runtime 消息转发 Method_Swizzling 动态添加方法 动态方法解析
- ios开发之-继承的实现运用
- Ios开发之 -- js和ios的交互
- 【读书笔记】iOS-截屏功能的实现。
- 华为:鸿蒙OS不是安卓和iOS的拷贝;胡润世界500强榜发布:苹果第一,腾讯第六
- 几乎是默写出来,加上自己理解的博客(iOS面试)