2012-07-15 36 views
2

我很喜歡將這種軟件組合用於一些簡單的推送通知。Phonegap + jQueryMobile + iOS上的PushNotifications

發送和接收不是問題 - 做到了!

但我怎麼能告訴iOS的jQueryMobile應用程序,它由PushNotification啓動,不應該顯示主屏幕,而是顯示其他 - 通知相關的屏幕?

回答

0

Notification對象有一個屬性applicationLaunchNotification其值1如果通過通知啓動應用程序。

這下面方法查詢應用的開始掛起通知:

window.plugins.pushNotification.getPendingNotifications(function(notifications) { 
    if(notifications.length > 0){ 
      var note = notifications[0]; 
      if(note.applicationLaunchNotification == "1"){ 
       // do the processing 
      } 
    } 
}); 

對於細節 - https://github.com/phonegap/phonegap-plugins/tree/master/iOS/PushNotification

+0

這不是重點。問題是,如何告訴應用程序的HTML部分,有新的通知並切換到不同的屏幕? – PascalTurbo 2012-07-17 07:14:41

0

使用可以改變你的應用程序deligate代碼,並能夠在應用促vising輪廓 推#import「AppDelegate.h」 #import「MainViewController.h」

#import <Cordova/CDVPlugin.h> 

    @implementation AppDelegate 

    @synthesize window, viewController; 

    - (id)init 
    { 
     /** If you need to do any extra app-specific initialization, you can do it here 
     * -jm 
     **/ 
     NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 

     [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 

     self = [super init]; 
     return self; 
    } 

    #pragma mark UIApplicationDelegate implementation 

    /** 
    * This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up) 
    */ 
    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
    { 
     CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

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

     self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease]; 
     self.window.autoresizesSubviews = YES; 

     self.viewController = [[[MainViewController alloc] init] autorelease]; 
     self.viewController.useSplashScreen = YES; 

     self.window.rootViewController = self.viewController; 
     [self.window makeKeyAndVisible]; 

     return YES; 
    } 

    // this happens while we are running (in the background, or from within our own app) 
    // only valid if __TESTING__-Info.plist specifies a protocol to handle 
    - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url 
    { 
     if (!url) { 
      return NO; 
     } 

     // calls into javascript global function 'handleOpenURL' 
     NSString* jsString = [NSString stringWithFormat:@"handleOpenURL(\"%@\");", url]; 
     [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString]; 

     // all plugins will get the notification, and their handlers will be called 
     [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]]; 

     return YES; 
    } 

    // repost the localnotification using the default NSNotificationCenter so multiple plugins may respond 
    // ADD OUR NOTIFICATION CODE 

    #pragma mark - Push notification Delegate Methods 

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    { 
     NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:  [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
     token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     //NSLog(@"content---%@", token); 
    } 


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

     //flagPushConfirmation=[NSString stringWithFormat:@"startPush"]; 
     UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error" 
        message:@"Device not Register for notification" 
       delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil]; 
     [message show]; 

    } 

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    { 
     UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Notification" 
      message:@"Recieve Notification" delegate:self cancelButtonTitle:@"cancel"otherButtonTitles:@"ok",nil]; 
     [message show]; 
    } 


    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
    { 

     UIApplicationState state = [application applicationState]; 
     if (state == UIApplicationStateActive) { 
      // WAS RUNNING 
      NSLog(@"I was currently active"); 

      NSString *notCB = [notification.userInfo objectForKey:@"foreground"]; 
      NSString *notID = [notification.userInfo objectForKey:@"notificationId"]; 

      NSString * jsCallBack = [NSString 
            stringWithFormat:@"%@(%@)", notCB,notID]; 


      [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; 

      application.applicationIconBadgeNumber = 0; 
     } 
     else { 
      // WAS IN BG 
      NSLog(@"I was in the background"); 

      NSString *notCB = [notification.userInfo objectForKey:@"background"]; 
      NSString *notID = [notification.userInfo objectForKey:@"notificationId"]; 

      NSString * jsCallBack = [NSString 
            stringWithFormat:@"%@(%@)", notCB,notID]; 
      [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; 

      application.applicationIconBadgeNumber = 0; 
     } 
    } 
    - (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window 
    { 
     // iPhone doesn't support upside down by default, while the iPad does. Override to allow all orientations always, and let the root view controller decide what's allowed (the supported orientations mask gets intersected). 
     NSUInteger supportedInterfaceOrientations = (1 << UIInterfaceOrientationPortrait) | (1 << UIInterfaceOrientationLandscapeLeft) | (1 << UIInterfaceOrientationLandscapeRight) | (1 << UIInterfaceOrientationPortraitUpsideDown); 

     return supportedInterfaceOrientations; 
    } 
相關問題