我使用推送通知创建了一个Phonegap 3.0应用。我主要关注博客here。 Android推送工作完美。但是我在iOS上接收推送通知时遇到问题。当应用程序在后台运行时,警报会出现,用户点击它,应用程序打开并发出ajax请求以重新发送消息。
但是,当应用程序未运行时,无论是在前台还是后台,警报都会出现,单击它时,应用程序会打开,但会立即冻结。
有一个类似的问题here我认为可以解决我的问题。但是,我的Phonegap 3.0项目中的didFinishLaunchingWithOptions()中的代码看起来与问题中的代码完全不同。我在AppDelegate.m中的代码如下:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
#endif
self.window.autoresizesSubviews = YES;
#if __has_feature(objc_arc)
self.viewController = [[MainViewController alloc] init];
#else
self.viewController = [[[MainViewController alloc] init] autorelease];
#endif
// Set your app's start page by setting the <content src='foo.html' /> tag in config.xml.
// If necessary, uncomment the line below to override it.
// self.viewController.startPage = @"index.html";
// NOTE: To customize the view's frame size (which defaults to full screen), override
// [self.viewController viewWillAppear:] in your view controller.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
我想也许didFinishLaunchingWithOptions的不同代码的原因是因为我使用的是Phonegap 3.0。但我在2.9中创建了一个项目,代码与3.0相同。其他问题的代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//commented out because it makes the app crash at startup with local notification...
/*NSArray *keyArray = [launchOptions allKeys];
if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil)
{
NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
NSLog(@"Mosa_fr_en-busi launchOptions = %@",url);
}*/
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
处理通知的javascript就像这样:
onDeviceReady: function() {
var pushNotification = window.plugins.pushNotification;
if (window.device.platform == 'android' || window.device.platform == 'Android') {
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});
}
else{
//so its apple
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
},
和
onNotificationAPN: function(event) {
console.log(event.article_id);
if ( event.article_id )
{
console.log('in event.article_id');
window.location.hash = "article/"+event.article_id;
//localStorage.payload =// event.payload
}
var pushNotification = window.plugins.pushNotification;
if (event.alert) {
console.log('in event.alert');
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log('in event.badge');
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
console.log('in event.sound');
var snd = new Media(event.sound);
snd.play();
}
},
实际上可以让Phonegap ios推送通知完全正常工作吗?
答案 0 :(得分:0)
我通过在安装后拔掉设备来解决这个问题。当仍然连接到xcode时,它导致应用程序在第二次打开时冻结。