当应用程序未连接到xcode时,iOS 8静默推送通知不会触发didReceiveRemoteNotification方法

时间:2014-12-24 19:18:40

标签: ios objective-c iphone xcode apple-push-notifications

如果设备未连接到xcode,我看到太多关于静音推送通知不起作用的问题,但我找不到真正的答案。 我使用Silent APN在后台启动进程,然后触发本地Push通知

  1. 服务器发送此信息:

    "_metadata" =     {
        bagde = 1;
        pushText = "semeone has sent you a message!!";
        sender = "semeone";
    };
    aps =     {
        "content-available" = 1;
    };
    

    _metadata是用于触发本地通知的自定义信息,我没有在aps中包含徽章,pushText ..因为我是静音推送通知。

  2. 客户端应该在didReceiveRemoteNotification中获取信息,

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
    if(application.applicationState != UIApplicationStateActive ){
            if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification
            {
                //start a background task 
                UIBackgroundTaskIdentifier preLoadPNTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                    NSLog(@"Background task to start a process ");
                }];
                //end completionHandler regarding to fetchCompletionHandler
                completionHandler(UIBackgroundFetchResultNewData);
    
                // doing my process...... and fire a local notification
    
                if(preLoadPNTask){
                    NSLog(@"End Background task ");
                    [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask];
                    preLoadPNTask = 0;
                }
                return;
            }
            else
            {
                NSLog(@"didReceiveRemoteNotification it's NOT the silent notification ");
                completionHandler(UIBackgroundFetchResultNoData);
                return;
            }
    
        }
    else {
        if(preLoadPNTask){
            NSLog(@"End Background task ");
            [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask];
            preLoadPNTask = 0;
        }
        completionHandler(UIBackgroundFetchResultNewData);
    }
    
    }
    
  3. 当设备连接到xcode时它完全正常,但是当它没有时,didReceiveRemoteNotification不会启动:(

    有什么想法吗?

    提前谢谢!!

3 个答案:

答案 0 :(得分:0)

我最终得到的是有线电视USB导致我出现一些问题,显然我每次插入iphone设备都说“这个配件可能不受支持”但是它继续正常工作,所以我换了一个新的,但这不能解决我的问题,但可以成为其中的一部分。所以我查看了代码,并且我做了一些更改,在收到2个更多的静默推送通知后,preLoadPNTask(UIBackgroundTaskIdentifier)创建了很多次,所以我在启动前添加了验证,

if(!preLoadPNTask){
//start a background task 
            UIBackgroundTaskIdentifier preLoadPNTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                NSLog(@"Background task to start a process ");
            }];

 }

我希望这对你有所帮助 此致

答案 1 :(得分:0)

在ios 8中,您需要执行以下步骤来触发didReceiveRemoteNotification:方法

  1. 选择项目目标转到功能选项卡
  2. 选择“背景模式”开启。
  3. 它会在您的项目info.plist
  4. 中添加一个键(必需的后台模式)

    完成这些修改后,当您收到苹果推送通知并且应用程序已在后台时,则会触发didReceiveRemoteNotification。

答案 2 :(得分:0)

可能是因为在iOS 8下你必须以不同的方式询问推送通知。试试这个:

-(void) registerForPushNotifications {

UIApplication* application=[UIApplication sharedApplication] ;

// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

} else {
    // Register for Push Notifications before iOS 8
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}

}