相关疑难解决方法(0)

检测是否通过推送通知启动/打开了应用程序

是否可以知道应用程序是否通过推送通知启动/打开?

我猜这个发布活动可以在这里找到:

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

    if (launchOptions != nil) {
         // Launched from push notification
         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当应用程序处于后台时,如何检测到它是从推送通知中打开的?

push-notification apple-push-notifications uiapplicationdelegate ios ios6

162
推荐指数
10
解决办法
11万
查看次数

didReceiveRemoteNotification:fetchCompletionHandler:从图标vs推送通知打开

我正在尝试实现后台推送通知处理,但是我在确定用户是否从发送的推送通知中打开应用程序时遇到了问题,而不是从图标打开它.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    //************************************************************
    // I only want this called if the user opened from swiping the push notification. 
    // Otherwise I just want to update the local model
    //************************************************************
    if(applicationState != UIApplicationStateActive) {
        MPOOpenViewController *openVc = [[MPOOpenViewController alloc] init];
        [self.navigationController pushViewController:openVc animated:NO];
    } else {
        ///Update local model
    }

    completionHandler(UIBackgroundFetchResultNewData);
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,无论用户如何打开应用程序,应用程序都会打开MPOOpenViewController.我怎样才能使视图控制器只有在他们通过刷通知打开应用程序时才会被推送?

使用相同的代码,这适用于iOS 6,但使用新的iOS 7方法,它的行为并不像我想要的那样.

编辑:我正在尝试在iOS 7上运行应用程序,我们不支持iOS 7之前的任何版本.我在方法的iOS 6版本中使用了相同的确切代码(没有完成处理程序)并且它表现得很好我希望它的方式.你要刷通知,这将被调用.如果从图标打开,则永远不会调用该方法.

push-notification apple-push-notifications uiapplicationdelegate ios ios7

50
推荐指数
3
解决办法
4万
查看次数

didReceiveRemoteNotification调用了两次

我在我的应用程序中实现了推送通知.

当我的应用程序在前台时,我的应用程序正常工作.

但当应用程序在后台或被杀时,我的didReceiveRemoteNotification被叫两次.

我已经提出了一个处理Push通知并从didFinishLaunchingWithOptions和 调用此方法的常用方法didReceiveRemoteNotification

这是我的实施:

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

  NSDictionary *pushDictionary = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if (pushDictionary) {
    [self customPushHandler:pushDictionary];
  }

  return YES;

}
Run Code Online (Sandbox Code Playgroud)

而且:

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
  fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {


         [self customPushHandler:userInfo];

 }
Run Code Online (Sandbox Code Playgroud)

和:

 - (void) customPushHandler:(NSDictionary *)notification {

     // Code to push ViewController

         NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner

   }
Run Code Online (Sandbox Code Playgroud)

当我的应用程序运行时,ViewController被推送一次.当我打开我的应用程序从通知横幅,然后我的屏幕被推了两次.

我放置了一个NSLog,customPushHandler当App处于前台时,我得到了一次,当我从Notification banner中启动时,我得到了两次.

我的代码中有什么问题.

iphone objective-c push-notification ios xcode6

8
推荐指数
2
解决办法
3227
查看次数