这是appdelegate.m获取设备令牌同样如何写入index.js中的phonegap
Appdelegate.m
(BOOL) application: (UIApplication * ) application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions {
CGRect screenBounds = [
[UIScreen mainScreen] bounds
];
[
[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; - (void) application: (UIApplication * ) application didReceiveRemoteNotification: (NSDictionary * ) userInfo {} - (void) application: (UIApplication * ) application didRegisterForRemoteNotificationsWithDeviceToken: (NSData * ) deviceToken {
NSString * devtoken = [NSString stringWithFormat: @"%@", deviceToken];
NSLog(@"the devicetoken is %@", devtoken);
NSRange r1 = [devtoken rangeOfString: @"<"];
NSRange r2 = [devtoken rangeOfString: @">"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString * sub = [devtoken substringWithRange: rSub];
NSLog(@"%@", sub);
NSString * newString = [sub stringByReplacingOccurrencesOfString: @" "
withString: @""
];
UIAlertView * message = [
[UIAlertView alloc] initWithTitle: @"Device Token!"
message: newString
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil
];
[message show];
NSLog(@"%@", newString);
} - (void) application: (UIApplication * ) application didFailToRegisterForRemoteNotificationsWithError: (NSError * ) error {
NSLog(@"the reason for fail to register the remote notifications is %@", error);
}
答案 0 :(得分:0)
您可以在js文件中获取设备令牌,例如
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
DLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%@", deviceToken);
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSMutableDictionary *results = [PushNotification getRemoteNotificationStatus];
[results setValue:token forKey:@"deviceToken"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:results];
[self writeJavascript:[pluginResult toSuccessCallbackString:[self.callbackIds valueForKey:@"registerDevice"]]];
}
在上面的代码中,pluginResult将被传递,并且我们可以在回调函数中获得deviceToken。
请从以下链接找到推送通知插件