如果应用程序在后台运行,为什么未调用didReceiveRemoteNotification

时间:2019-06-21 10:53:27

标签: objective-c pushy

我正在尝试使用pushy.me向我的应用发送推送通知。当应用程序在后台运行时,除非收到通知横幅或应用程序在前台,否则我无法对收到的数据进行任何处理。

这是我的通知有效载荷

   {
  "registration_ids": [
  "692d1e6c16d483a4613193"
   ],
   "data": {
   "title": "Test Notification",
   "message": "hello"
  },
  "notification": {
    "body": "Hello World ✌",
    "badge": 1,
    "content-available" : "1",
   "mutable-content" : "1",
    "sound": "ping.aiff"
  }
}

代码:

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

    NSLog(@"%@", pushInfo);

    if(application.applicationState == UIApplicationStateInactive) {

        NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
        //do some tasks
      //  [self manageRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    else if (application.applicationState == UIApplicationStateBackground) {
        //NOT WORKING
        NSLog(@"application Background - notification has arrived when app was in background");
        NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[pushInfo valueForKey:@"aps"] valueForKey:@"content_available"]];


        if([contentAvailable isEqualToString:@"1"]) {
            // do tasks


        //    [self manageRemoteNotification:userInfo];
            NSLog(@"content-available is equal to 1");
        //    completionHandler(UIBackgroundFetchResultNewData);
        }
    }
    else {
        NSLog(@"application Active - notication has arrived while app was opened");
        //Show an in-app banner
        //do tasks
     //   [self performFetchWithCompletionHandler];
        completionHandler(UIBackgroundFetchResultNewData);
    }
}

0 个答案:

没有答案