【问题标题】:iOS 8 Silent Push Notification doesn't fire didReceiveRemoteNotification method when application not connected to xcode当应用程序未连接到 xcode 时,iOS 8 静默推送通知不会触发 didReceiveRemoteNotification 方法
【发布时间】:2014-12-24 19:18:40
【问题描述】:

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

  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);
    }
    
    }
    

当设备连接到 xcode 时它工作得很好,但是当它没有连接时,didReceiveRemoteNotification 不会启动:(

有什么想法吗?

提前谢谢你!!

【问题讨论】:

  • 收到通知后你在执行什么任务?
  • 我将 ID 和发件人添加到 NSMutableDictionary,然后我将数据发送到 Worklight [[WL sharedInstance] sendActionToJS:@"silentNotification" withData:dictionary];然后我释放本地通知 localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 徽章; localNotif.alertBody = textAlert; localNotif.userInfo = userInfoData; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif 发布];
  • 怎么知道是否调用了didReceiveRemoteNotification方法?
  • 我和你@jan遇到了同样的情况。到现在都解决不了。 (iOS 8.1.2)
  • 我已经在 iOS 7 和 iOS 8 设备上进行了测试。我在 iOS 8 设备上遇到了同样的问题,但不知道发生了什么。在我的 iOS 7 设备上,一切正常——接收到静默通知(有或没有 xcode)。在我的 iOS 8 设备上,根本没有收到静默通知(没有 Xcode)。不过,它可以 100% 使用 Xcode。如果你们中的任何人找到解决方案,请告诉我。

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


【解决方案1】:

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

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

 }

希望对你有帮助 问候

【讨论】:

  • 还要记得在3分钟前关闭preLoadPNTask(BackgroundTask),否则应用会崩溃
【解决方案2】:

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

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

在这些修改之后,当您收到苹果推送通知并且应用程序已经在后台时,didReceiveRemoteNotification 将被触发。

【讨论】:

    【解决方案3】:

    可能是因为在 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)];
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多