gpt4 book ai didi

apple-push-notifications - 适用于 Adob​​e Air 的推送通知 iOS native 扩展

转载 作者:行者123 更新时间:2023-12-04 08:44:14 25 4
gpt4 key购买 nike

我正在为 Adob​​e AIR 开发 iOS native 扩展,它将获取推送通知的设备 token 。不幸的是,我不是那种喜欢 objective-C 的程序员,我不确定我使用的代码是否有问题。它编译没有问题,我可以将扩展与 AIR 一起使用,但看起来注册通知不会返回正面或负面影响。所以我想做的是在从 AIR 调用 RegisterDevice 函数时注册通知,如果它确实注册,则将设备 token 存储在 deviceTokenString 中,如果它没有注册并返回错误,我将错误存储在此字符串中.当调用 GetToken 函数时,我将 deviceTokenString 返回给 AIR,因此它是 token 或错误。在 AIR 应用程序中,我首先启动 RegisterDevice 函数,然后通过单击按钮启动 GetToken 函数。不幸的是,我既没有得到 token 也没有得到错误(也没有出现请求许可的弹出窗口)。我也试图将注册部分放在 didFinishLaunchingWithOptions 中,但看起来 didFinishLaunchingWithOptions 从未启动过。如果你们能看看代码是否可以,我将非常感激。或者,也许您有任何想法还有什么可能是错误的?我在配置门户中启用了推送 SSL 证书。这是我正在使用的代码

"NativePush.m":

#import "UIKit/UIKit.h"
#import "include/FlashRuntimeExtensions.h"

@implementation NativePush

@synthesize tokenString = _tokenString;

NSString *deviceTokenString = @"";

- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}

return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *str =
[NSString stringWithFormat:@"%@",deviceToken];
deviceTokenString = str;
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
deviceTokenString = str;
}

void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet) {
*numFunctionsToTest = 2;
FRENamedFunction* func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*2);
func[0].name = (const uint8_t*)"RegisterDevice";
func[0].functionData = NULL;
func[0].function = &RegisterDevice;

func[1].name = (const uint8_t*)"GetToken";
func[1].functionData = NULL;
func[1].function = &GetToken;

*functionsToSet = func;
}

void ContextFinalizer(FREContext ctx) {
return;
}

void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet,
FREContextFinalizer* ctxFinalizerToSet) {
*extDataToSet = NULL;
*ctxInitializerToSet = &ContextInitializer;
*ctxFinalizerToSet = &ContextFinalizer;
}

void ExtFinalizer(void* extData) {
return;
}

FREObject RegisterDevice(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {

[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];

return NULL;
}

FREObject GetToken(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {

NSString* tokenS = deviceTokenString;

char* tokenChar = [tokenS UTF8String];
FREObject tokenObject = NULL;

FRENewObjectFromUTF8( strlen(tokenChar)+1 , (const uint8_t*)tokenChar, &tokenObject);

return tokenObject;

}

@end

和头文件“NativePush.h”:

import "Foundation/Foundation.h"
import "include/FlashRuntimeExtensions.h"

@interface NativePush : NSObject
@property (nonatomic, retain) NSString* tokenString;

FREObject RegisterDevice(
FREContext ctx,
void* funcData,
uint32_t argc,
FREObject arg[]
);
FREObject GetToken(
FREContext ctx,
void* funcData,
uint32_t argc,
FREObject arg[]
);
void ContextInitializer(
void* extData,
const uint8_t* ctxType,
FREContext ctx,
uint32_t* numFunctionsToTest,
const FRENamedFunction** functionsToSet
);
void ContextFinalizer(FREContext ctx);
void ExtInitializer(
void** extDataToSet,
FREContextInitializer* ctxInitializerToSet,
FREContextFinalizer* ctxFinalizerToSet
);
void ExtFinalizer(void* extData);
@end

最佳答案

好的,

在撕掉我的头发 3 天后,我弄明白了。我没有设置代表,因为那会破坏 Adob​​e 的所有东西。我创建了现有委托(delegate)的自定义子类,并覆盖了与 APNS 相关的委托(delegate)函数。我的代码如下。

//empty delegate functions, stubbed signature is so we can find this method in the delegate
//and override it with our custom implementation
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{}


//custom implementations of empty signatures above
void didRegisterForRemoteNotificationsWithDeviceToken(id self, SEL _cmd, UIApplication* application, NSData* deviceToken)
{
NSLog(@"My token is: %@", deviceToken);
}


void didFailToRegisterForRemoteNotificationsWithError(id self, SEL _cmd, UIApplication* application, NSError* error)
{
NSLog(@"Failed to get token, error: %@", error);
}





// ContextInitializer()
//
// The context initializer is called when the runtime creates the extension context instance.
void PushContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{

//injects our modified delegate functions into the sharedApplication delegate

id delegate = [[UIApplication sharedApplication] delegate];

Class objectClass = object_getClass(delegate);

NSString *newClassName = [NSString stringWithFormat:@"Custom_%@", NSStringFromClass(objectClass)];
Class modDelegate = NSClassFromString(newClassName);
if (modDelegate == nil) {
// this class doesn't exist; create it
// allocate a new class
modDelegate = objc_allocateClassPair(objectClass, [newClassName UTF8String], 0);

SEL selectorToOverride1 = @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:);

SEL selectorToOverride2 = @selector(application:didFailToRegisterForRemoteNotificationsWithError:);

// get the info on the method we're going to override
Method m1 = class_getInstanceMethod([jreporterNativePush class], selectorToOverride1);
Method m2 = class_getInstanceMethod([jreporterNativePush class], selectorToOverride2);

// add the method to the new class
class_addMethod(modDelegate, selectorToOverride1, (IMP)didRegisterForRemoteNotificationsWithDeviceToken, method_getTypeEncoding(m1));

class_addMethod(modDelegate, selectorToOverride2, (IMP)didFailToRegisterForRemoteNotificationsWithError, method_getTypeEncoding(m2));

// register the new class with the runtime
objc_registerClassPair(modDelegate);
}
// change the class of the object
object_setClass(delegate, modDelegate);


NSLog(@"completed crazy swap w/o bombing w00t");

///////// end of delegate injection / modification code



*numFunctionsToTest = 1;

FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * 1);

func[0].name = (const uint8_t*) "registerPush";
func[0].functionData = NULL;
func[0].function = &registerPush;

*functionsToSet = func;

关于apple-push-notifications - 适用于 Adob​​e Air 的推送通知 iOS native 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com