在Phonegap + jQuery Mobile + iOS上推送通知

时间:2012-07-15 12:48:02

标签: ios jquery-mobile cordova apple-push-notifications

我很乐意将这种软件组合用于一些简单的推送通知。

发送和接收不是问题 - 做到了!

但是我怎么能告诉iOS中的jQueryMobile应用程序它是由PushNotification启动的,不应该显示主屏幕而是显示另一个 - 通知相关的屏幕呢?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用可以更改您的应用程序解析代码并启用推送应用程序管理配置文件         #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;
    }