【发布时间】:2014-01-24 16:52:11
【问题描述】:
我创建了一个带有推送通知的 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 推送通知完全工作?
【问题讨论】:
标签: objective-c cordova push-notification apple-push-notifications