gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:24 25 4
gpt4 key购买 nike

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

  1. 服务器发送此信息:

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

    _metadata 是触发本地通知的自定义信息,我没有在 aps 中包含 badge、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 不会启动 :(

有什么想法吗?

提前致谢!

最佳答案

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

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

}

希望对你有帮助问候

关于当应用程序未连接到 xcode 时,iOS 8 静默推送通知不会触发 didReceiveRemoteNotification 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27641338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com