Phonegap + jQueryMobile + iOS 上的 PushNotifications

Posted

技术标签:

【中文标题】Phonegap + jQueryMobile + iOS 上的 PushNotifications【英文标题】:PushNotifications on Phonegap + jQueryMobile + iOS 【发布时间】:2012-07-15 12:48:01 【问题描述】:

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

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

但是我如何才能告诉 ios 之外的 jQueryMobile 应用程序它是由 PushNotification 启动的,并且不应该显示主屏幕而是显示其他与通知相关的屏幕?

【问题讨论】:

【参考方案1】:

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

【讨论】:

这不是重点。问题是,如何告诉应用程序的 html 部分,有一个新的通知并切换到不同的屏幕?【参考方案2】:

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

【讨论】:

以上是关于Phonegap + jQueryMobile + iOS 上的 PushNotifications的主要内容,如果未能解决你的问题,请参考以下文章

使用 JqueryMobile 在 phoneGap 中启动应用程序时调用服务器

在 Android / PhoneGap 上运行的 jQueryMobile 拒绝 .load / .ajax

jQueryMobile + PhoneGap + iOS 11:阻止尝试使用history.replaceState()更改会话历史记录URL

使用手势的 iOS Phonegap 滑动检测

在“PhoneGap + jQuery Mobile”应用程序中正确注册事件

Phonegap App:从屏幕边缘滑动