doumu9019 2015-10-21 09:07
浏览 76
已采纳

我的有效负载是否格式错误,在后台调用didReceiveRemoteNotification?

According to these (and many more) questions on SO:

I figured out, that the main keys for silent push-notification and for the method

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


to be called in the background are: the payload, iOS version > iOS 7, enabled background mode with "Remote notifications" and registering push notifications in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.

I did all of those things, but the method mentioned above, that supposed to be called in the background, is only being called while the app is foreground. This is how I form my payload in php:

$contents = json_encode(array(
            'aps'   => array(
                'content-available' => '1',
                'sound' => '',
                'alert' => '',
            ),
            'where' => array(
                'deviceType'    => array(
                '$in'   =>  array('ios', 'android'),
                ),
            ),
            'data'  => array(
                'title' => $this->subject,
                'msg'   => $this->message,
                'date'  => date('d.m.Y H:i', time() + (60 * 60 * 2)),
                'key'   => APPKEY,
                'ids'   => $ids,
            ),

        ));

and this is what I get in Xcode console, as I am able to NSLog the payload when app is in foreground:

{
    aps =     {
    };
    date = "21.10.2015 11:01";
    ids =     (
        259,
        257,
        256,
    );
    key = xpy3fq4t3wn9;
    msg = test11;
    title = aatest1;
}

What am I doing wrong? Why isnt "content-available = 1" in Xcode console? Or is there any other reason why appDelegate method isnt being called in background?

Im using iOS 8.1.3, Parse as Push-Notification provider.

  • 写回答

1条回答 默认 最新

  • doubi4340 2015-10-21 13:26
    关注

    I was able to solve my problem just by trying different payload combinations, ended up with this one:

    $contents = json_encode(array(
    
                'where' => array(
                    'deviceType'    => array(
                    '$in'   =>  array('ios', 'android'),
                    ),
                ),
                'data'  => array(
                    'title' => $this->subject,
                    'msg'   => $this->message,
                    'date'  => date('d.m.Y H:i', time() + (60 * 60 * 2)),
                    'key'   => APPKEY,
                    'ids'   => $ids,
                    'aps'   => array(
                         'content-available'    => '1',
                         'sound'    => '',
                         'alert' => '',
                     ),
                ),
    
            ));
    

    Looks like 'aps' has to be in 'data' array. Now the app delegate method is being called in background and I can see "content-available = 1" in Xcode console.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料