【问题标题】:Push notification method get call at the time of launching the app推送通知方法在启动应用程序时调用
【发布时间】:2016-07-24 06:05:46
【问题描述】:

每当用户单击推送通知时,都会调用此方法
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) 但如果应用程序正在启动并且同时推送来调用相同的方法。如何识别应用程序是否正在启动或用户单击图标以使应用程序处于前台。 这样我就可以忽略推送通知过程。

【问题讨论】:

    标签: ios push-notification apple-push-notifications


    【解决方案1】:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (launchOptions != nil)
        {
            // Here app will open from pushnotification
            //RemoteNotification
            NSDictionary* dictionary1 = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
            //LocalNotification
            NSDictionary* dictionary2 = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
            if (dictionary1 != nil)
            {
                //RemoteNotification Payload
                //set your function to handle notification
                NSLog(@"Launched from push notification: %@", dictionary1);
            }
            if (dictionary2 != nil)
            {
                NSLog(@"Launched from dictionary2dictionary2dictionary2 notification: %@", dictionary2);
                double delayInSeconds = 7;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    // [self addMessageFromRemoteNotification:dictionary2 updateUI:NO];
                });
            }
    
         }
         else
             {}
    

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

     if(application.applicationState == UIApplicationStateInactive) {
    
            NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
            //do some tasks
    
        }
        else if (application.applicationState == UIApplicationStateBackground) {
    
            NSLog(@"application Background - notification has arrived when app was in background");
            NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];
    
            if([contentAvailable isEqualToString:@"1"]) {
                // do tasks
                NSLog(@"content-available is equal to 1");
            }
        }
        else {
            NSLog(@"application Active - notication has arrived while app was opened");
            //Show an in-app banner
            //do tasks
        }
    

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多