-2

我正在尝试很多但尚未成功在应用程序被终止状态时获得静默通知

这是我正在尝试的代码..

APS 数据:

{
  "aps": {
          "content-available": 1,
          "sound": ""
         }

}


    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSDictionary *userInfo1 = userInfo;
NSLog(@"userInfo: %@", userInfo1);

//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.

if (application.applicationState == UIApplicationStateActive)
{
    //opened from a push notification when the app was on background

    NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was Running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertView show];


}
else
{
    // a push notification when the app is running. So that you can display an alert and push in any view

    NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]);
      [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo];
}
}
4

2 回答 2

2

在有效负载的aps字典中不得包含警报、声音或徽章键。

{
    "aps":{
        "content-available" : 1
    }
}

请试试这个。

于 2017-05-31T11:01:48.063 回答
0

您应该按如下方式实施AppDelegate.m

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSDictionary *aps = [userInfo objectForKey:@"aps"];
    NSLog(@"hello");
    NSLog(@"userinfo---->>>>%@",userInfo);
    [UIApplication sharedApplication].applicationIconBadgeNumber=[[aps objectForKey:@"badge"] integerValue];
    [self application:application didReceiveRemoteNotification:userInfo];
}
于 2017-05-31T09:26:41.293 回答